From 05b6f140246637730c50099220e92265a35b6d35 Mon Sep 17 00:00:00 2001 From: Paladz Date: Thu, 17 Jun 2021 19:07:27 +0800 Subject: [PATCH 1/1] edit neg op (#1971) Co-authored-by: paladz --- protocol/vm/ops.go | 2 -- protocol/vm/pushdata.go | 8 -------- protocol/vm/pushdata_test.go | 17 ---------------- protocol/vm/types_test.go | 13 ++++++++++++ protocol/vm/vm.go | 47 +++++++++++++++++++------------------------- 5 files changed, 33 insertions(+), 54 deletions(-) diff --git a/protocol/vm/ops.go b/protocol/vm/ops.go index d59e016e..fd168f15 100644 --- a/protocol/vm/ops.go +++ b/protocol/vm/ops.go @@ -226,8 +226,6 @@ var ( OP_PUSHDATA2: {OP_PUSHDATA2, "PUSHDATA2", opPushdata}, OP_PUSHDATA4: {OP_PUSHDATA4, "PUSHDATA4", opPushdata}, - OP_1NEGATE: {OP_1NEGATE, "1NEGATE", op1Negate}, - OP_NOP: {OP_NOP, "NOP", opNop}, // control flow diff --git a/protocol/vm/pushdata.go b/protocol/vm/pushdata.go index 1d28e352..51485332 100644 --- a/protocol/vm/pushdata.go +++ b/protocol/vm/pushdata.go @@ -20,14 +20,6 @@ func opPushdata(vm *virtualMachine) error { return vm.push(d, false) } -func op1Negate(vm *virtualMachine) error { - err := vm.applyCost(1) - if err != nil { - return err - } - return vm.pushInt64(-1, false) -} - func opNop(vm *virtualMachine) error { return vm.applyCost(1) } diff --git a/protocol/vm/pushdata_test.go b/protocol/vm/pushdata_test.go index 659ea751..b37d1eca 100644 --- a/protocol/vm/pushdata_test.go +++ b/protocol/vm/pushdata_test.go @@ -31,23 +31,6 @@ func TestPushdataOps(t *testing.T) { dataStack: [][]byte{}, }, wantErr: ErrRunLimitExceeded, - }, { - op: OP_1NEGATE, - startVM: &virtualMachine{ - runLimit: 50000, - dataStack: [][]byte{}, - }, - wantVM: &virtualMachine{ - runLimit: 49983, - dataStack: [][]byte{Int64Bytes(-1)}, - }, - }, { - op: OP_1NEGATE, - startVM: &virtualMachine{ - runLimit: 1, - dataStack: [][]byte{}, - }, - wantErr: ErrRunLimitExceeded, }} pushdataops := []Op{OP_PUSHDATA1, OP_PUSHDATA2, OP_PUSHDATA4} diff --git a/protocol/vm/types_test.go b/protocol/vm/types_test.go index 2f38352e..777ed2c6 100644 --- a/protocol/vm/types_test.go +++ b/protocol/vm/types_test.go @@ -6,6 +6,8 @@ import ( "testing" "github.com/holiman/uint256" + + "github.com/bytom/bytom/testutil" ) func TestBoolBytes(t *testing.T) { @@ -159,3 +161,14 @@ func TestAsBigInt(t *testing.T) { } } } + +func TestInt64BigIntConvert(t *testing.T) { + cases := []int64{0, 1, 2, 1024, 65536, 9223372036854775807} + for i, c := range cases { + x := Int64Bytes(c) + y := BigIntBytes(uint256.NewInt().SetUint64(uint64(c))) + if !testutil.DeepEqual(x, y) { + t.Errorf("case %d fail on compare %d bytes", i, c) + } + } +} diff --git a/protocol/vm/vm.go b/protocol/vm/vm.go index 898e0f0f..74cc1b58 100644 --- a/protocol/vm/vm.go +++ b/protocol/vm/vm.go @@ -59,17 +59,15 @@ func Verify(context *Context, gasLimit int64) (gasLeft int64, err error) { runLimit: gasLimit, context: context, } - stateData := context.StateData - for i, state := range stateData { + + for i, state := range context.StateData { if err = vm.pushAlt(state, false); err != nil { return vm.runLimit, errors.Wrapf(err, "pushing initial statedata %d", i) } } - args := context.Arguments - for i, arg := range args { - err = vm.push(arg, false) - if err != nil { + for i, arg := range context.Arguments { + if err = vm.push(arg, false); err != nil { return vm.runLimit, errors.Wrapf(err, "pushing initial argument %d", i) } } @@ -79,7 +77,7 @@ func Verify(context *Context, gasLimit int64) (gasLeft int64, err error) { err = ErrFalseVMResult } - return vm.runLimit, wrapErr(err, vm, args) + return vm.runLimit, wrapErr(err, vm, context.Arguments) } // falseResult returns true iff the stack is empty or the top @@ -90,8 +88,7 @@ func (vm *virtualMachine) falseResult() bool { func (vm *virtualMachine) run() error { for vm.pc = 0; vm.pc < uint32(len(vm.program)); { // handle vm.pc updates in step - err := vm.step() - if err != nil { + if err := vm.step(); err != nil { return err } } @@ -125,16 +122,15 @@ func (vm *virtualMachine) step() error { vm.deferredCost = 0 vm.data = inst.Data - err = ops[inst.Op].fn(vm) - if err != nil { + if err = ops[inst.Op].fn(vm); err != nil { return err } - err = vm.applyCost(vm.deferredCost) - if err != nil { + + if err = vm.applyCost(vm.deferredCost); err != nil { return err } - vm.pc = vm.nextPC + vm.pc = vm.nextPC if TraceOut != nil { for i := len(vm.dataStack) - 1; i >= 0; i-- { fmt.Fprintf(TraceOut, " stack %d: %x\n", len(vm.dataStack)-1-i, vm.dataStack[i]) @@ -148,12 +144,10 @@ func (vm *virtualMachine) push(data []byte, deferred bool) error { cost := 8 + int64(len(data)) if deferred { vm.deferCost(cost) - } else { - err := vm.applyCost(cost) - if err != nil { - return err - } + } else if err := vm.applyCost(cost); err != nil { + return err } + vm.dataStack = append(vm.dataStack, data) return nil } @@ -162,14 +156,11 @@ func (vm *virtualMachine) pushAlt(data []byte, deferred bool) error { cost := 8 + int64(len(data)) if deferred { vm.deferCost(cost) - } else { - err := vm.applyCost(cost) - if err != nil { - return err - } + } else if err := vm.applyCost(cost); err != nil { + return err } - vm.altStack = append(vm.altStack, data) + vm.altStack = append(vm.altStack, data) return nil } @@ -207,8 +198,8 @@ func (vm *virtualMachine) popInt64(deferred bool) (int64, error) { if err != nil { return 0, err } - n, err := AsInt64(bytes) - return n, err + + return AsInt64(bytes) } func (vm *virtualMachine) popBigInt(deferred bool) (*uint256.Int, error) { @@ -224,6 +215,7 @@ func (vm *virtualMachine) top() ([]byte, error) { if len(vm.dataStack) == 0 { return nil, ErrDataStackUnderflow } + return vm.dataStack[len(vm.dataStack)-1], nil } @@ -233,6 +225,7 @@ func (vm *virtualMachine) applyCost(n int64) error { vm.runLimit = 0 return ErrRunLimitExceeded } + vm.runLimit -= n return nil } -- 2.11.0