github.com/intfoundation/intchain@v0.0.0-20220727031208-4316ad31ca73/consensus/ipbft/types/tendermint_test.go (about) 1 package types 2 3 import ( 4 "encoding/hex" 5 "fmt" 6 "github.com/intfoundation/intchain/common/hexutil" 7 "github.com/intfoundation/go-crypto" 8 "github.com/intfoundation/go-wire" 9 "math/big" 10 "testing" 11 "time" 12 ) 13 14 var extraHex = "0x0108696e74636861696e0000000000002c0615cd7d205898b7400000000000000000000201149d2b5214e24104f648819a2b851970e02e0f73c50114dae66c6bca991012f6f62d4db42c79c60e3c2e44010114980aaa734720bdc9d93e3d4030a135aec4910bac0000000000000001011415a43260d301c016d636ec117eac56578f1157990000000000002c06000140439243a34a9ef90323f7db46d7800b7049fa4a50599956054d716de1cf37f7dc686c8a5c6510a17609cda05dd53737cc8635f15da4f7f48ccc1c288db0960e730100000000000000010101000000000000000100" 15 var extraHex2 = "0x0108696e74636861696e000000000003713d15d1c7c6c2488b8000000000000000000020011415d14546048c69022a12e0c8605338e68a841fb30114ec299e752fd4b41f0ea3d5a4e2b350e6f3a1471b010114ca95018425ce58d2e041eb5d9eda115f181214f800000000000000010114f4fd5d4e44794f67fa797f7824dd706934970e3c000000000003713d010201405678a97feace6079d60753d636ad7358349fcd2b222e1ffff393359f2c272e5d132f435b18967010e881db0dc52055c8c50ff0ac660195c494aa85a7015c7de30100000000000000030101000000000000000300" 16 var blsPubkeyHex = "0x31A9A1B8808146B846E8D919F9BF565F3F18E1F5359101B05BB41A28761F671411D824C15794553ACC74E85E5B6E67919B7C4CCFB844AC21611DD9AD2B78AA2069B988F89E9C2C6DA57A68960584D0346FFD910823DE06C55D7B151AECBBC4731F76DDA5E7202BB1EAF824F065EB43E187D14CC1EBC5C69033D42D03F699D8F2" 17 18 func TestEncodeTendermintExtra(t *testing.T) { 19 var extra = TendermintExtra{} 20 extra = TendermintExtra{ 21 ChainID: "intchain", 22 Height: uint64(11270), 23 Time: time.Now(), 24 NeedToSave: false, 25 NeedToBroadcast: false, 26 EpochNumber: uint64(2), 27 SeenCommitHash: []byte{0x3e, 0x78, 0x73, 0xef, 0xaf, 0x71, 0x6, 0xda, 0x71, 0x62, 0x68, 0xbe, 0x31, 0xd2, 0x73, 0xf, 0xa4, 0x28, 0x35, 0xda}, 28 ValidatorsHash: []byte{0xda, 0xe6, 0x6c, 0x6b, 0xca, 0x99, 0x10, 0x12, 0xf6, 0xf6, 0x2d, 0x4d, 0xb4, 0x2c, 0x79, 0xc6, 0xe, 0x3c, 0x2e, 0x44}, 29 SeenCommit: &Commit{}, 30 EpochBytes: []byte{}, 31 } 32 extraHash := extra.Hash() 33 fmt.Printf("extraHash=%v\n", hex.EncodeToString(extraHash)) 34 35 } 36 37 func TestValidator_Hash(t *testing.T) { 38 var blsPubKey crypto.BLSPubKey 39 blsPubKeyByte, err := hexutil.Decode(blsPubkeyHex) 40 if err != nil { 41 t.Error(err) 42 } 43 fmt.Printf("blsPubKeyByte len=%v\n", len(blsPubKeyByte)) 44 for i, v := range blsPubKeyByte { 45 blsPubKey[i] = v 46 } 47 fmt.Printf("blsPubKey%v\n", blsPubKey) 48 validator := Validator{ 49 Address: []byte("32H8py5Jg396p7QNDUwTwkeVod15ksxne5"), 50 PubKey: blsPubKey, 51 VotingPower: big.NewInt(10000000000), 52 RemainingEpoch: uint64(0), 53 } 54 55 validatorHash := validator.Hash() 56 fmt.Printf("validatorHash=%v\n", validatorHash) 57 fmt.Printf("validatorHashHex=%v\n", hexutil.Encode(validatorHash)) 58 } 59 60 //type val struct { 61 // Address string `json:"address"` 62 // PublicKey string `json:"public_key"` 63 // RemainEpoch string `json:"remain_epoch"` 64 // VotingPower string `json:"voting_power"` 65 //} 66 //func TestValidatorSet_Hash(t *testing.T) { 67 // var valSet ValidatorSet 68 // var validators []*Validator 69 // var valList = []*val{ 70 // { 71 // Address: "INT3CpFuk2cJ1te9WZV1w8Y3wkQCcA5Z", 72 // PublicKey: "0x3589CA45DFD8EFB4103F8FB59BD3185B0BF73DC1EB8B659B0DDB2AFDEDB2BC433D89AF7791019456C79943113CA1370C9CA0B9DC341D884C5DDEDF0056E4057C45EB54DD9007917007C804BFBA3DF6E29D59CC43EFC7C3C10057C99818AEBECC21D8466741547D7CFCB709AF6293C338701EA82B2E4FC8F03188A47F35F261C6", 73 // RemainEpoch:"0x0", 74 // VotingPower: "0x2540be400", 75 // }, 76 // { 77 // Address: "INT38cqij9EtpxCZk9wrRY8u5U9reZAp", 78 // PublicKey: "0x49DC3C606826D11FB38463197E3184DA388D829FC95835D4891C42CDCD0AACA84E65220F3034BF2B3C83497D90432C4A1B960F255613B062DFD3BD36CD9A061788EF399B142AAA8E3852206E72FF525DB76FBBDCC9F0FDB5C1C4028CE5D5421D3C334D893B67C90EC9DF4FD6D14C48F37DB133ACA561D4732D172DE3D41086F2", 79 // RemainEpoch:"0x0", 80 // VotingPower: "0x54b40b1f852bda000000", 81 // }, 82 // //{ 83 // // Address: "INT3MUHiVzxaNdG1RAD7zQimzSZBtErX", 84 // // PublicKey: "0x372EC175F6D52B91FF43493AC9E113790C0CC6AD6562A1AD7E581717C40F017A6587F3FED9DFA590CED46AE37B99617F06DB3C36BE68F1AB9005276439AB44A455DDE3C1472467D84F851B9974D46A6A525E24638D5101BE207258D91983C03A2FB021C4C50DFE95E6169C698879ED9EA4A6089B24C596C17F46489823EA7CF7", 85 // // RemainEpoch:"0x0", 86 // // VotingPower: "0x54b40b1f852bda000000", 87 // //}, 88 // } 89 // 90 // var totalVP = big.NewInt(0) 91 // validators = make([]*Validator, len(valList)) 92 // for i, v := range valList { 93 // vp, _ := hexutil.DecodeBig(v.VotingPower) 94 // validators[i], _ = makeValidator(v.Address, v.PublicKey, vp) 95 // totalVP.Add(totalVP, vp) 96 // } 97 // 98 // valSet = ValidatorSet{ 99 // validators, 100 // totalVP, 101 // } 102 // 103 // fmt.Printf("validatorset validators %v\n", valSet.Validators) 104 // fmt.Printf("validatorset validatorset hash %v\n", valSet.Hash()) 105 // fmt.Printf("validatorset validatorset hash hex %v\n", hexutil.Encode(valSet.Hash())) 106 // 107 //} 108 109 func TestDecodeTendermintExtra(t *testing.T) { 110 var extra = TendermintExtra{} 111 extraByte, err := hexutil.Decode(extraHex2) 112 fmt.Printf("extraByte=%v\n\n", extraByte) 113 if err != nil { 114 t.Error(err) 115 } 116 err = wire.ReadBinaryBytes(extraByte, &extra) 117 if err != nil { 118 t.Error(err) 119 } 120 fmt.Printf("extra=%v\n", extra) 121 fmt.Printf("extra.validatorshash=%v\n", extra.ValidatorsHash) 122 fmt.Printf("extra.validatorshashhex=%v\n\n", hexutil.Encode(extra.ValidatorsHash)) 123 fmt.Printf("extra.SeenCommit=%v\n", *extra.SeenCommit) 124 fmt.Printf("extra.SeenCommit.BitArray=%v\n", extra.SeenCommit.BitArray) 125 fmt.Printf("extra.SeenCommit.NumCommits=%v\n", extra.SeenCommit.NumCommits()) 126 } 127 128 //func TestDecodeValidatorsHash(t *testing.T) { 129 // //var validatorsHashStr = "2uZsa8qZEBL29i1NtCx5xg48LkQ=" 130 // var validatorsHashStr = "7CmedS/UtB8Oo9Wk4rNQ5vOhRxs=" 131 // var validators Validator 132 // 133 // data, err := base64.StdEncoding.DecodeString(validatorsHashStr) 134 // if err != nil { 135 // t.Error(err) 136 // } 137 // fmt.Printf("validatorshash=%v\n", data) 138 // fmt.Printf("validatorshashhex=%v\n", hexutil.Encode(data)) 139 // 140 // // 解不出来 validator 141 // err = wire.ReadBinaryBytes(data, &validators) 142 // if err != nil { 143 // t.Error(err) 144 // } 145 // fmt.Printf("validator=%v\n", validators) 146 //} 147 148 //func makeValidator(address, blsPubKey string, vp *big.Int) (validator *Validator, err error){ 149 // var blsPK crypto.BLSPubKey 150 // blsPubKeyByte, err := hexutil.Decode(blsPubKey) 151 // if err != nil { 152 // return validator, err 153 // } 154 // 155 // for i, v := range blsPubKeyByte { 156 // blsPK[i] = v 157 // } 158 // 159 // validator = &Validator{ 160 // Address: []byte(address), 161 // PubKey: blsPK, 162 // VotingPower: vp, 163 // RemainingEpoch: uint64(0), 164 // } 165 // 166 // return validator, nil 167 //}