github.com/igggame/nebulas-go@v2.1.0+incompatible/nf/nvm/engine_bankvault_test.go (about) 1 package nvm 2 3 import ( 4 "encoding/json" 5 "io/ioutil" 6 "testing" 7 8 "github.com/nebulasio/go-nebulas/consensus/dpos" 9 "github.com/nebulasio/go-nebulas/core" 10 "github.com/nebulasio/go-nebulas/core/state" 11 "github.com/nebulasio/go-nebulas/storage" 12 "github.com/nebulasio/go-nebulas/util/byteutils" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestBankVaultContract(t *testing.T) { 17 type TakeoutTest struct { 18 args string 19 expectedErr error 20 beforeBalance string 21 afterBalance string 22 } 23 24 tests := []struct { 25 name string 26 contractPath string 27 sourceType string 28 saveValue string 29 saveArgs string 30 takeoutTests []TakeoutTest 31 }{ 32 {"deploy bank_vault_contract.js", "./test/bank_vault_contract.js", "js", "5", "[0]", 33 []TakeoutTest{ 34 {"[1]", nil, "5", "4"}, 35 {"[5]", core.ErrExecutionFailed, "4", "4"}, 36 {"[4]", nil, "4", "0"}, 37 {"[1]", core.ErrExecutionFailed, "0", "0"}, 38 }, 39 }, 40 {"deploy bank_vault_contract.ts", "./test/bank_vault_contract.ts", "ts", "5", "[0]", 41 []TakeoutTest{ 42 {"[1]", nil, "5", "4"}, 43 {"[5]", core.ErrExecutionFailed, "4", "4"}, 44 {"[4]", nil, "4", "0"}, 45 {"[1]", core.ErrExecutionFailed, "0", "0"}, 46 }, 47 }, 48 } 49 50 for _, tt := range tests { 51 t.Run(tt.name, func(t *testing.T) { 52 data, err := ioutil.ReadFile(tt.contractPath) 53 assert.Nil(t, err, "contract path read error") 54 55 mem, _ := storage.NewMemoryStorage() 56 context, _ := state.NewWorldState(dpos.NewDpos(), mem) 57 owner, err := context.GetOrCreateUserAccount([]byte("account1")) 58 assert.Nil(t, err) 59 owner.AddBalance(newUint128FromIntWrapper(10000000)) 60 61 // prepare the contract. 62 addr, err := core.NewContractAddressFromData([]byte("n1FkntVUMPAsESuCAAPK711omQk19JotBjM"), byteutils.FromUint64(1)) 63 assert.Nil(t, err) 64 contract, _ := context.CreateContractAccount(addr.Bytes(), nil, nil) 65 contract.AddBalance(newUint128FromIntWrapper(5)) 66 67 // parepare env, block & transactions. 68 tx := mockNormalTransaction("n1FkntVUMPAsESuCAAPK711omQk19JotBjM", "n1JNHZJEUvfBYfjDRD14Q73FX62nJAzXkMR", tt.saveValue) 69 ctx, err := NewContext(mockBlock(), tx, contract, context) 70 71 // execute. 72 engine := NewV8Engine(ctx) 73 engine.SetExecutionLimits(10000, 100000000) 74 _, err = engine.DeployAndInit(string(data), tt.sourceType, "") 75 assert.Nil(t, err) 76 engine.Dispose() 77 78 // call save. 79 engine = NewV8Engine(ctx) 80 engine.SetExecutionLimits(10000, 100000000) 81 _, err = engine.Call(string(data), tt.sourceType, "save", tt.saveArgs) 82 assert.Nil(t, err) 83 engine.Dispose() 84 85 var ( 86 bal struct { 87 Balance string `json:"balance"` 88 } 89 ) 90 91 // call takeout. 92 for _, tot := range tt.takeoutTests { 93 // call balanceOf. 94 engine = NewV8Engine(ctx) 95 engine.SetExecutionLimits(10000, 100000000) 96 balance, err := engine.Call(string(data), tt.sourceType, "balanceOf", "") 97 assert.Nil(t, err) 98 bal.Balance = "" 99 err = json.Unmarshal([]byte(balance), &bal) 100 assert.Nil(t, err) 101 assert.Equal(t, tot.beforeBalance, bal.Balance) 102 engine.Dispose() 103 104 engine = NewV8Engine(ctx) 105 engine.SetExecutionLimits(10000, 100000000) 106 _, err = engine.Call(string(data), tt.sourceType, "takeout", tot.args) 107 assert.Equal(t, err, tot.expectedErr) 108 engine.Dispose() 109 110 // call balanceOf. 111 engine = NewV8Engine(ctx) 112 engine.SetExecutionLimits(10000, 100000000) 113 balance, err = engine.Call(string(data), tt.sourceType, "balanceOf", "") 114 assert.Nil(t, err) 115 bal.Balance = "" 116 err = json.Unmarshal([]byte(balance), &bal) 117 assert.Nil(t, err) 118 assert.Equal(t, tot.afterBalance, bal.Balance) 119 engine.Dispose() 120 } 121 }) 122 } 123 }