You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.5 KiB

  1. // Copyright 2017-2018 DERO Project. All rights reserved.
  2. // Use of this source code in any form is governed by RESEARCH license.
  3. // license can be found in the LICENSE file.
  4. // GPG: 0F39 E425 8C65 3947 702A 8234 08B2 0360 A03A 9DE8
  5. //
  6. //
  7. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  8. // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  9. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
  10. // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  11. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  12. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  13. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  15. // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. package checkpoints
  17. // generate main net checkpoints automatically from file mainnet_checkpoints.dat
  18. //go:generate sh -c "echo // Code generated by go generate DO NOT EDIT. > mainnet_checkpoints.go"
  19. //go:generate sh -c "echo // This file contains all the mainnet checkpoints > mainnet_checkpoints.go"
  20. //go:generate sh -c "echo // please read checkpoints.go comments > mainnet_checkpoints.go"
  21. //go:generate sh -c "echo package checkpoints >> mainnet_checkpoints.go"
  22. //go:generate sh -c "echo >> mainnet_checkpoints.go"
  23. //go:generate sh -c "echo var mainnet_checkpoints = []byte{ >> mainnet_checkpoints.go"
  24. //go:generate sh -c "xxd -i < mainnet_checkpoints.dat >> mainnet_checkpoints.go"
  25. //go:generate sh -c "truncate -s-1 mainnet_checkpoints.go"
  26. //go:generate sh -c "echo ,} >> mainnet_checkpoints.go"
  27. //go:generate sh -c "echo // Code generated by go generate DO NOT EDIT. > testnet_checkpoints.go"
  28. //go:generate sh -c "echo // This file contains all the testnet checkpoints > testnet_checkpoints.go"
  29. //go:generate sh -c "echo // please read checkpoints.go comments > testnet_checkpoints.go"
  30. //go:generate sh -c "echo package checkpoints >> testnet_checkpoints.go"
  31. //go:generate sh -c "echo >> testnet_checkpoints.go"
  32. // generate blank file if no checkpoints
  33. //go:generate sh -c "if [ ! -s testnet_checkpoints.dat ]; then echo var testnet_checkpoints = []byte{} >> testnet_checkpoints.go; fi;"
  34. //go:generate sh -c " if [ -s testnet_checkpoints.dat ]; then echo var testnet_checkpoints = []byte{ >> testnet_checkpoints.go; fi"
  35. //go:generate sh -c "if [ -s testnet_checkpoints.dat ]; then xxd -i < testnet_checkpoints.dat >> testnet_checkpoints.go;fi"
  36. //go:generate sh -c "if [ -s testnet_checkpoints.dat ]; then truncate -s-1 testnet_checkpoints.go; fi"
  37. //go:generate sh -c "if [ -s testnet_checkpoints.dat ]; then echo ,} >> testnet_checkpoints.go;fi "
  38. import "fmt"
  39. import "github.com/romana/rlog"
  40. import "github.com/arnaucode/derosuite/crypto"
  41. import "github.com/arnaucode/derosuite/globals"
  42. // this file handles and maintains checkpoints which are used by blockchain to skip some checks on known parts of the chain
  43. // the skipping of the checks can be disabled by command line arguments
  44. var mainnet_checkpoints_height uint64 = uint64(len(mainnet_checkpoints) / 32) // each checkpoint is 32 bytes in size
  45. var testnet_checkpoints_height uint64 = uint64(len(testnet_checkpoints) / 32) // each checkpoint is 32 bytes in size
  46. func init() {
  47. rlog.Tracef(1, "Loaded %d checkpoints for mainnet hardcoded", mainnet_checkpoints_height)
  48. rlog.Tracef(1, "Loaded %d checkpoints for testnet hardcoded", testnet_checkpoints_height)
  49. }
  50. // gives length of currently available checkpoints
  51. func Length() uint64 {
  52. switch globals.Config.Name {
  53. case "mainnet":
  54. return mainnet_checkpoints_height
  55. case "testnet":
  56. return testnet_checkpoints_height
  57. default:
  58. return 0
  59. }
  60. // we can never reach here
  61. //return 0
  62. }
  63. // tell whether a checkpoint is known in the current selected network
  64. func IsCheckPointKnown(hash crypto.Hash, height uint64) (result bool) {
  65. var known_hash crypto.Hash
  66. switch globals.Config.Name {
  67. case "mainnet":
  68. if height < mainnet_checkpoints_height {
  69. copy(known_hash[:], mainnet_checkpoints[32*height:])
  70. if known_hash == hash {
  71. result = true
  72. return
  73. }
  74. }
  75. case "testnet":
  76. if height < testnet_checkpoints_height {
  77. copy(known_hash[:], testnet_checkpoints[32*height:])
  78. if known_hash == hash {
  79. result = true
  80. return
  81. }
  82. }
  83. default:
  84. panic(fmt.Sprintf("Unknown Network \"%s\"", globals.Config.Name))
  85. }
  86. return
  87. }