github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/test/bcrp_test.go (about) 1 package test 2 3 import ( 4 "bytes" 5 "encoding/hex" 6 "os" 7 "testing" 8 9 "github.com/google/uuid" 10 11 "github.com/bytom/bytom/crypto/sha3pool" 12 "github.com/bytom/bytom/database" 13 dbm "github.com/bytom/bytom/database/leveldb" 14 "github.com/bytom/bytom/errors" 15 "github.com/bytom/bytom/protocol/vm" 16 ) 17 18 func TestRegisterContract(t *testing.T) { 19 dbName := uuid.New().String() 20 db := dbm.NewDB(dbName, "leveldb", dbName) 21 defer os.RemoveAll(dbName) 22 23 store := database.NewStore(db) 24 chain, _, _, _ := mockChainWithStore(store) 25 26 contract, err := hex.DecodeString("0164740a52797b937b788791698700c0") 27 if err != nil { 28 t.Fatal(err) 29 } 30 31 if err := registerContract(chain, store, contract); err != nil { 32 t.Fatal(err) 33 } 34 35 var hash [32]byte 36 sha3pool.Sum256(hash[:], contract) 37 data := db.Get(database.CalcContractKey(hash)) 38 if data == nil { 39 t.Fatalf("can't find the registered contract by contract hash %v", hash) 40 } 41 42 if !bytes.Equal(contract, data[32:]) { 43 t.Fatalf("TestRegisterContract failed: got %v want %v", data[32:], contract) 44 } 45 } 46 47 func TestUseContractSuccess(t *testing.T) { 48 dbName := uuid.New().String() 49 db := dbm.NewDB(dbName, "leveldb", dbName) 50 defer os.RemoveAll(dbName) 51 52 store := database.NewStore(db) 53 chain, _, _, _ := mockChainWithStore(store) 54 55 contract, err := hex.DecodeString("0164740a52797b937b788791698700c0") 56 if err != nil { 57 t.Fatal(err) 58 } 59 60 if err := registerContract(chain, store, contract); err != nil { 61 t.Fatal(err) 62 } 63 64 arguments := [][]byte{ 65 {byte(99)}, 66 {byte(1)}, 67 } 68 69 if err := validateContract(chain, contract, arguments, nil); err != nil { 70 t.Fatal(err) 71 } 72 } 73 74 func TestUseContractFailed(t *testing.T) { 75 dbName := uuid.New().String() 76 db := dbm.NewDB(dbName, "leveldb", dbName) 77 defer os.RemoveAll(dbName) 78 79 store := database.NewStore(db) 80 chain, _, _, _ := mockChainWithStore(store) 81 82 contract, err := hex.DecodeString("0164740a52797b937b788791698700c0") 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 if err := registerContract(chain, store, contract); err != nil { 88 t.Fatal(err) 89 } 90 91 arguments := [][]byte{ 92 {byte(99)}, 93 {byte(2)}, 94 } 95 96 if err := validateContract(chain, contract, arguments, nil); errors.Root(err) != vm.ErrFalseVMResult { 97 t.Fatal(err) 98 } 99 } 100 101 func TestUseContractWithStateDataSuccess(t *testing.T) { 102 dbName := uuid.New().String() 103 db := dbm.NewDB(dbName, "leveldb", dbName) 104 defer os.RemoveAll(dbName) 105 106 store := database.NewStore(db) 107 chain, _, _, _ := mockChainWithStore(store) 108 109 contract, err := hex.DecodeString("01646c7c740a52797b937b788791698700c0") 110 if err != nil { 111 t.Fatal(err) 112 } 113 114 if err := registerContract(chain, store, contract); err != nil { 115 t.Fatal(err) 116 } 117 118 arguments := [][]byte{ 119 {byte(1)}, 120 } 121 122 stateData := [][]byte{ 123 {byte(99)}, 124 } 125 126 if err := validateContract(chain, contract, arguments, stateData); err != nil { 127 t.Fatal(err) 128 } 129 } 130 131 func TestUseContractWithStateDataFailed(t *testing.T) { 132 dbName := uuid.New().String() 133 db := dbm.NewDB(dbName, "leveldb", dbName) 134 defer os.RemoveAll(dbName) 135 136 store := database.NewStore(db) 137 chain, _, _, _ := mockChainWithStore(store) 138 139 contract, err := hex.DecodeString("01646c7c740a52797b937b788791698700c0") 140 if err != nil { 141 t.Fatal(err) 142 } 143 144 if err := registerContract(chain, store, contract); err != nil { 145 t.Fatal(err) 146 } 147 148 arguments := [][]byte{ 149 {byte(2)}, 150 } 151 152 stateData := [][]byte{ 153 {byte(99)}, 154 } 155 156 if err := validateContract(chain, contract, arguments, stateData); errors.Root(err) != vm.ErrFalseVMResult { 157 t.Fatal(err) 158 } 159 }