diff --git a/protocol/vm/types.go b/protocol/vm/types.go index 99b0c98d8..771d71a32 100644 --- a/protocol/vm/types.go +++ b/protocol/vm/types.go @@ -62,9 +62,10 @@ func bigIntInt64(n *uint256.Int) (int64, error) { // reverse []byte. func reverse(b []byte) []byte { - for i, j := 0, len(b)-1; i < j; i, j = i+1, j-1 { - b[i], b[j] = b[j], b[i] + r := make([]byte, len(b)) + copy(r, b) + for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 { + r[i], r[j] = r[j], r[i] } - - return b + return r } diff --git a/protocol/vm/types_test.go b/protocol/vm/types_test.go index 626a7ca8d..1e75f8098 100644 --- a/protocol/vm/types_test.go +++ b/protocol/vm/types_test.go @@ -3,6 +3,7 @@ package vm import ( "bytes" "math/big" + "reflect" "testing" "github.com/holiman/uint256" @@ -136,3 +137,49 @@ func TestInt64BigIntConvert(t *testing.T) { } } } + +func Test_reverse(t *testing.T) { + type args struct { + b []byte + } + type wants struct { + origin []byte + want []byte + } + tests := []struct { + name string + args args + wants wants + }{ + { + name: "test reverse", + args: args{ + b: []byte{0x00, 0x00, 0x00, 0x00, 0x01}, + }, + wants: wants{ + origin: []byte{0x00, 0x00, 0x00, 0x00, 0x01}, + want: []byte{0x01, 0x00, 0x00, 0x00, 0x00}, + }, + }, + { + name: "test reverse 1", + args: args{ + b: []byte{0x01, 0x02, 0x20, 0x03, 0x01}, + }, + wants: wants{ + origin: []byte{0x01, 0x02, 0x20, 0x03, 0x01}, + want: []byte{0x01, 0x03, 0x20, 0x02, 0x01}, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := reverse(tt.args.b); !reflect.DeepEqual(got, tt.wants.want) { + t.Errorf("reverse() = %v, want %v", got, tt.wants.want) + } + if !reflect.DeepEqual(tt.args.b, tt.wants.origin) { + t.Errorf("after reverse args = %v, origin %v", tt.args.b, tt.wants.origin) + } + }) + } +} diff --git a/protocol/vm/vmutil/script_test.go b/protocol/vm/vmutil/script_test.go index db794d795..eefb9f7b7 100644 --- a/protocol/vm/vmutil/script_test.go +++ b/protocol/vm/vmutil/script_test.go @@ -3,6 +3,7 @@ package vmutil import ( "crypto/ed25519" "encoding/hex" + "reflect" "testing" "github.com/bytom/bytom/errors" @@ -211,10 +212,16 @@ func TestGetIssuanceProgramRestrictHeight(t *testing.T) { if err != nil { t.Fatal(err) } - + originProgram := make([]byte, len(program)) + copy(originProgram, program) gotHeight := GetIssuanceProgramRestrictHeight(program) if gotHeight != test.wantHeight { t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: got %d want %d", i, gotHeight, test.wantHeight) + return + } + + if !reflect.DeepEqual(originProgram, program) { + t.Errorf("TestGetIssuanceProgramRestrictHeight #%d failed: after got %v before %v", i, program, originProgram) } } }