github.com/annchain/OG@v0.0.9/vm/vm_test/delegate_call_test.go (about) 1 package vm_test 2 3 import ( 4 "fmt" 5 "github.com/annchain/OG/common" 6 "github.com/annchain/OG/common/math" 7 "github.com/annchain/OG/vm/eth/core/vm" 8 "github.com/annchain/OG/vm/ovm" 9 "github.com/sirupsen/logrus" 10 "github.com/stretchr/testify/assert" 11 "testing" 12 "time" 13 ) 14 15 func TestCall(t *testing.T) { 16 from := common.HexToAddress("0xABCDEF88") 17 from2 := common.HexToAddress("0xABCDEF87") 18 coinBase := common.HexToAddress("0x1234567812345678AABBCCDDEEFF998877665544") 19 20 tracer := vm.NewStructLogger(&vm.LogConfig{ 21 Debug: true, 22 }) 23 ldb := DefaultLDB(from, coinBase) 24 ldb.CreateAccount(from2) 25 ldb.AddBalance(from2, math.NewBigInt(10000000)) 26 27 rt := &Runtime{ 28 Tracer: tracer, 29 VmContext: ovm.NewOVMContext(&ovm.DefaultChainContext{}, &coinBase, ldb), 30 TxContext: &ovm.TxContext{ 31 From: common.HexToAddress("0xABCDEF88"), 32 Value: math.NewBigInt(0), 33 GasPrice: math.NewBigInt(1), 34 GasLimit: DefaultGasLimit, 35 Coinbase: coinBase, 36 SequenceID: 0, 37 }, 38 } 39 40 addrs := make(map[string]common.Address) 41 42 for _, filename := range []string{"Callee.bin", "Caller.bin"} { 43 _, contractAddr, leftGas, err := DeployContract(filename, from, coinBase, rt, nil) 44 assert.NoError(t, err) 45 addrs[filename] = contractAddr 46 dump(t, ldb, nil, leftGas, err) 47 48 } 49 50 logrus.Info("delegatecall") 51 { 52 params := EncodeParams([]interface{}{ 53 addrs["Callee.bin"], 54 0xD0D0, 55 }) 56 ret, leftGas, err := CallContract(addrs["Caller.bin"], from, coinBase, rt, math.NewBigInt(0), "9207ba9a", params) 57 dump(t, ldb, ret, leftGas, err) 58 } 59 //vm.WriteTrace(os.Stdout, tracer.Logs) 60 61 rt.Tracer = vm.NewStructLogger(&vm.LogConfig{ 62 Debug: true, 63 }) 64 65 logrus.Info("call") 66 { 67 params := EncodeParams([]interface{}{ 68 addrs["Callee.bin"], 69 0xB0B0, 70 }) 71 ret, leftGas, err := CallContract(addrs["Caller.bin"], from, coinBase, rt, math.NewBigInt(0), "fdfa868f", params) 72 dump(t, ldb, ret, leftGas, err) 73 } 74 time.Sleep(time.Second * 3) 75 //rt.Tracer.Write(os.Stdout) 76 77 //logrus.Info("callcode") 78 ////vm.WriteTrace(os.Stdout, tracer.Logs) 79 //{ 80 // params := EncodeParams([]interface{}{ 81 // addrs["Callee.bin"], 82 // 0xC0C0, 83 // }) 84 // ret, leftGas, err := CallContract(addrs["Caller.bin"], from2, coinBase, rt, math.NewBigInt(0), "0eebdd95", params) 85 // dump(t, ldb, ret, leftGas, err) 86 //} 87 88 //vm.WriteTrace(os.Stdout, tracer.Logs) 89 90 ldb.MergeChanges() 91 fmt.Println("Merged") 92 fmt.Println(ldb.String()) 93 94 }