Skip to content

Commit 45fecd3

Browse files
author
ZhangTao1596
committed
vm: use uint256.Int (fixes #1581)
1 parent 9f9c6f5 commit 45fecd3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+882
-486
lines changed

cli/nep_test/nep11_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import (
77
"encoding/json"
88
"fmt"
99
"io"
10-
"math/big"
1110
"os"
1211
"path/filepath"
1312
"strconv"
1413
"testing"
1514

15+
"github.com/holiman/uint256"
1616
"github.com/nspcc-dev/neo-go/internal/testcli"
1717
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
1818
"github.com/nspcc-dev/neo-go/pkg/core/state"
@@ -351,7 +351,7 @@ func TestNEP11_ND_OwnerOf_BalanceOf_Transfer(t *testing.T) {
351351
Name: "OnNEP11Payment",
352352
Item: stackitem.NewArray([]stackitem.Item{
353353
stackitem.NewByteArray(nftOwnerHash.BytesBE()),
354-
stackitem.NewBigInteger(big.NewInt(1)),
354+
stackitem.NewBigInteger(uint256.NewInt(1)),
355355
stackitem.NewByteArray(tokenID1),
356356
stackitem.NewByteArray([]byte("some_data")),
357357
}),
@@ -636,7 +636,7 @@ func TestNEP11_D_OwnerOf_BalanceOf_Transfer(t *testing.T) {
636636
Name: "OnNEP11Payment",
637637
Item: stackitem.NewArray([]stackitem.Item{
638638
stackitem.NewByteArray(validatorHash.BytesBE()),
639-
stackitem.NewBigInteger(big.NewInt(25)),
639+
stackitem.NewBigInteger(uint256.NewInt(25)),
640640
stackitem.NewByteArray(token2ID),
641641
stackitem.NewByteArray([]byte("some_data")),
642642
}),

cli/vm/cli_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"errors"
99
"fmt"
1010
gio "io"
11-
"math/big"
1211
"os"
1312
"path/filepath"
1413
"strconv"
@@ -18,6 +17,7 @@ import (
1817
"time"
1918

2019
"github.com/chzyer/readline"
20+
"github.com/holiman/uint256"
2121
"github.com/nspcc-dev/neo-go/cli/paramcontext"
2222
"github.com/nspcc-dev/neo-go/internal/basicchain"
2323
"github.com/nspcc-dev/neo-go/internal/random"
@@ -590,9 +590,9 @@ func TestRunWithDifferentArguments(t *testing.T) {
590590

591591
e.checkNextLine(t, "READY: loaded \\d.* instructions")
592592
e.checkStack(t, []stackitem.Item{
593-
stackitem.NewBigInteger(big.NewInt(1)),
594-
stackitem.NewBigInteger(big.NewInt(2)),
595-
stackitem.NewBigInteger(big.NewInt(3)),
593+
stackitem.NewBigInteger(uint256.NewInt(1)),
594+
stackitem.NewBigInteger(uint256.NewInt(2)),
595+
stackitem.NewBigInteger(uint256.NewInt(3)),
596596
})
597597
}
598598

go.work

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
go 1.20
2+
3+
use (
4+
.
5+
./pkg/interop
6+
)

pkg/compiler/for_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/holiman/uint256"
1011
"github.com/nspcc-dev/neo-go/pkg/compiler"
1112
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
1213
"github.com/nspcc-dev/neo-go/pkg/vm"
@@ -36,7 +37,7 @@ func TestEntryPointWithArgs(t *testing.T) {
3637
return 2 + args[1].(int)
3738
}
3839
`
39-
args := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(0)), stackitem.NewBigInteger(big.NewInt(1))}
40+
args := []stackitem.Item{stackitem.NewBigInteger(uint256.NewInt(0)), stackitem.NewBigInteger(uint256.NewInt(1))}
4041
evalWithArgs(t, src, nil, args, big.NewInt(3))
4142
}
4243

@@ -51,7 +52,7 @@ func TestEntryPointWithMethodAndArgs(t *testing.T) {
5152
return 0
5253
}
5354
`
54-
args := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(0)), stackitem.NewBigInteger(big.NewInt(1))}
55+
args := []stackitem.Item{stackitem.NewBigInteger(uint256.NewInt(0)), stackitem.NewBigInteger(uint256.NewInt(1))}
5556
evalWithArgs(t, src, []byte("foobar"), args, big.NewInt(3))
5657
}
5758

@@ -156,9 +157,9 @@ func TestIntArray(t *testing.T) {
156157
}
157158
`
158159
eval(t, src, []stackitem.Item{
159-
stackitem.NewBigInteger(big.NewInt(1)),
160-
stackitem.NewBigInteger(big.NewInt(2)),
161-
stackitem.NewBigInteger(big.NewInt(3)),
160+
stackitem.NewBigInteger(uint256.NewInt(1)),
161+
stackitem.NewBigInteger(uint256.NewInt(2)),
162+
stackitem.NewBigInteger(uint256.NewInt(3)),
162163
})
163164
}
164165

pkg/compiler/interop_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ func TestForcedNotifyArgumentsConversion(t *testing.T) {
586586
Name: strconv.Itoa(i),
587587
Type: targetSCParamTypes[i],
588588
}
589-
defaultValue := stackitem.NewBigInteger(big.NewInt(int64(i)))
589+
defaultValue := stackitem.NewBigIntegerFromInt64(int64(i))
590590
var (
591591
val stackitem.Item
592592
err error

pkg/compiler/native_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"math/big"
87
"strconv"
98
"strings"
109
"testing"
1110

11+
"github.com/holiman/uint256"
1212
"github.com/nspcc-dev/neo-go/pkg/compiler"
1313
"github.com/nspcc-dev/neo-go/pkg/config"
1414
"github.com/nspcc-dev/neo-go/pkg/core/interop"
@@ -362,7 +362,7 @@ func getTestStackItem(typ smartcontract.ParamType) stackitem.Item {
362362
case smartcontract.BoolType:
363363
return stackitem.NewBool(true)
364364
case smartcontract.IntegerType:
365-
return stackitem.NewBigInteger(big.NewInt(42))
365+
return stackitem.NewBigInteger(uint256.NewInt(42))
366366
case smartcontract.ByteArrayType, smartcontract.StringType, smartcontract.Hash160Type,
367367
smartcontract.Hash256Type, smartcontract.PublicKeyType, smartcontract.SignatureType:
368368
return stackitem.NewByteArray([]byte("result"))

pkg/compiler/slice_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/holiman/uint256"
1011
"github.com/nspcc-dev/neo-go/pkg/compiler"
1112
"github.com/nspcc-dev/neo-go/pkg/vm"
1213
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
@@ -162,11 +163,11 @@ var sliceTestCases = []testCase{
162163
}
163164
`,
164165
[]stackitem.Item{
165-
stackitem.NewBigInteger(big.NewInt(1)),
166-
stackitem.NewBigInteger(big.NewInt(2)),
167-
stackitem.NewBigInteger(big.NewInt(3)),
168-
stackitem.NewBigInteger(big.NewInt(4)),
169-
stackitem.NewBigInteger(big.NewInt(5)),
166+
stackitem.NewBigInteger(uint256.NewInt(1)),
167+
stackitem.NewBigInteger(uint256.NewInt(2)),
168+
stackitem.NewBigInteger(uint256.NewInt(3)),
169+
stackitem.NewBigInteger(uint256.NewInt(4)),
170+
stackitem.NewBigInteger(uint256.NewInt(5)),
170171
},
171172
},
172173
{

pkg/compiler/struct_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88
"testing"
99

10+
"github.com/holiman/uint256"
1011
"github.com/nspcc-dev/neo-go/pkg/compiler"
1112
"github.com/nspcc-dev/neo-go/pkg/vm"
1213
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
@@ -278,8 +279,8 @@ var structTestCases = []testCase{
278279
}
279280
`,
280281
[]stackitem.Item{
281-
stackitem.NewBigInteger(big.NewInt(1)),
282-
stackitem.NewBigInteger(big.NewInt(2)),
282+
stackitem.NewBigInteger(uint256.NewInt(1)),
283+
stackitem.NewBigInteger(uint256.NewInt(2)),
283284
stackitem.NewByteArray([]byte("hello")),
284285
stackitem.NewBool(false),
285286
},

pkg/compiler/syscall_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/holiman/uint256"
1112
"github.com/nspcc-dev/neo-go/pkg/compiler"
1213
"github.com/nspcc-dev/neo-go/pkg/core"
1314
"github.com/nspcc-dev/neo-go/pkg/core/interop"
@@ -201,7 +202,7 @@ func TestNotify(t *testing.T) {
201202
require.NoError(t, v.Run())
202203
require.Equal(t, 2, len(s.events))
203204

204-
exp0 := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(11)), stackitem.NewByteArray([]byte("sum")), stackitem.NewBigInteger(big.NewInt(12))}
205+
exp0 := []stackitem.Item{stackitem.NewBigInteger(uint256.NewInt(11)), stackitem.NewByteArray([]byte("sum")), stackitem.NewBigInteger(uint256.NewInt(12))}
205206
assert.Equal(t, "Event1", s.events[0].Name)
206207
assert.Equal(t, exp0, s.events[0].Item.Value())
207208
assert.Equal(t, "single", s.events[1].Name)

pkg/core/block/block.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"encoding/json"
55
"errors"
66
"math"
7-
"math/big"
87

8+
"github.com/holiman/uint256"
99
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
1010
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
1111
"github.com/nspcc-dev/neo-go/pkg/io"
@@ -223,14 +223,14 @@ func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int {
223223
func (b *Block) ToStackItem() stackitem.Item {
224224
items := []stackitem.Item{
225225
stackitem.NewByteArray(b.Hash().BytesBE()),
226-
stackitem.NewBigInteger(big.NewInt(int64(b.Version))),
226+
stackitem.NewBigInteger(uint256.NewInt(uint64(b.Version))),
227227
stackitem.NewByteArray(b.PrevHash.BytesBE()),
228228
stackitem.NewByteArray(b.MerkleRoot.BytesBE()),
229-
stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))),
230-
stackitem.NewBigInteger(new(big.Int).SetUint64(b.Nonce)),
231-
stackitem.NewBigInteger(big.NewInt(int64(b.Index))),
229+
stackitem.NewBigInteger(uint256.NewInt(uint64(b.Timestamp))),
230+
stackitem.NewBigInteger(uint256.NewInt(b.Nonce)),
231+
stackitem.NewBigInteger(uint256.NewInt(uint64(b.Index))),
232232
stackitem.NewByteArray(b.NextConsensus.BytesBE()),
233-
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
233+
stackitem.NewBigInteger(uint256.NewInt(uint64(len(b.Transactions)))),
234234
}
235235
if b.StateRootEnabled {
236236
items = append(items, stackitem.NewByteArray(b.PrevStateRoot.BytesBE()))

pkg/core/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ func (bc *Blockchain) handleNotification(note *state.NotificationEvent, d *dao.S
17361736
return
17371737
}
17381738
}
1739-
bc.processTokenTransfer(d, transCache, h, b, note.ScriptHash, from, to, amount, id)
1739+
bc.processTokenTransfer(d, transCache, h, b, note.ScriptHash, from, to, util.ToBig(amount), id)
17401740
}
17411741

17421742
func parseUint160(itm stackitem.Item) (util.Uint160, error) {

pkg/core/interop/contract/call.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package contract
33
import (
44
"errors"
55
"fmt"
6-
"math/big"
76
"strings"
87

8+
"github.com/holiman/uint256"
99
"github.com/nspcc-dev/neo-go/pkg/core/dao"
1010
"github.com/nspcc-dev/neo-go/pkg/core/interop"
1111
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
@@ -47,7 +47,7 @@ func LoadToken(ic *interop.Context, id int32) error {
4747
func Call(ic *interop.Context) error {
4848
h := ic.VM.Estack().Pop().Bytes()
4949
method := ic.VM.Estack().Pop().String()
50-
fs := callflag.CallFlag(int32(ic.VM.Estack().Pop().BigInt().Int64()))
50+
fs := callflag.CallFlag(int32(ic.VM.Estack().Pop().BigInt().Uint64()))
5151
if fs&^callflag.All != 0 {
5252
return errors.New("call flags out of range")
5353
}
@@ -176,6 +176,6 @@ func CallFromNative(ic *interop.Context, caller util.Uint160, cs *state.Contract
176176

177177
// GetCallFlags returns current context calling flags.
178178
func GetCallFlags(ic *interop.Context) error {
179-
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(ic.VM.Context().GetCallFlags()))))
179+
ic.VM.Estack().PushItem(stackitem.NewBigInteger(uint256.NewInt(uint64(ic.VM.Context().GetCallFlags()))))
180180
return nil
181181
}

pkg/core/interop/contract/call_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"testing"
1010

11+
"github.com/holiman/uint256"
1112
"github.com/nspcc-dev/neo-go/internal/contracts"
1213
"github.com/nspcc-dev/neo-go/internal/random"
1314
"github.com/nspcc-dev/neo-go/pkg/compiler"
@@ -189,7 +190,7 @@ func TestLoadToken(t *testing.T) {
189190

190191
t.Run("good", func(t *testing.T) {
191192
realBalance, _ := bc.GetGoverningTokenBalance(acc.ScriptHash())
192-
cInvoker.Invoke(t, stackitem.NewBigInteger(big.NewInt(realBalance.Int64()+1)), "callT0", acc.ScriptHash())
193+
cInvoker.Invoke(t, stackitem.NewBigInteger(uint256.NewInt(uint64(realBalance.Int64()+1))), "callT0", acc.ScriptHash())
193194
})
194195
t.Run("invalid param count", func(t *testing.T) {
195196
cInvoker.InvokeFail(t, "method not found: callT2/1", "callT2", acc.ScriptHash())

pkg/core/interop/iterator/interop_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (t *testIter) Next() bool {
2323
}
2424

2525
func (t testIter) Value() stackitem.Item {
26-
return stackitem.NewBigInteger(big.NewInt(int64(t.arr[t.index])))
26+
return stackitem.NewBigIntegerFromInt64(int64(t.arr[t.index]))
2727
}
2828

2929
// Iterator is thoroughly tested in VM package, these are smoke tests.

pkg/core/interop/runtime/engine.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package runtime
33
import (
44
"errors"
55
"fmt"
6-
"math/big"
76

7+
"github.com/holiman/uint256"
88
"github.com/nspcc-dev/neo-go/pkg/core/interop"
99
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
1010
"github.com/nspcc-dev/neo-go/pkg/vm"
@@ -65,7 +65,7 @@ func Platform(ic *interop.Context) error {
6565

6666
// GetTrigger returns the script trigger.
6767
func GetTrigger(ic *interop.Context) error {
68-
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(ic.Trigger))))
68+
ic.VM.Estack().PushItem(stackitem.NewBigInteger(uint256.NewInt(uint64(ic.Trigger))))
6969
return nil
7070
}
7171

@@ -110,7 +110,7 @@ func Notify(ic *interop.Context) error {
110110
// LoadScript takes a script and arguments from the stack and loads it into the VM.
111111
func LoadScript(ic *interop.Context) error {
112112
script := ic.VM.Estack().Pop().Bytes()
113-
fs := callflag.CallFlag(int32(ic.VM.Estack().Pop().BigInt().Int64()))
113+
fs := callflag.CallFlag(int32(ic.VM.Estack().Pop().BigInt().Uint64()))
114114
if fs&^callflag.All != 0 {
115115
return errors.New("call flags out of range")
116116
}
@@ -148,23 +148,20 @@ func Log(ic *interop.Context) error {
148148
// GetTime returns timestamp of the block being verified, or the latest
149149
// one in the blockchain if no block is given to Context.
150150
func GetTime(ic *interop.Context) error {
151-
ic.VM.Estack().PushItem(stackitem.NewBigInteger(new(big.Int).SetUint64(ic.Block.Timestamp)))
151+
ic.VM.Estack().PushItem(stackitem.NewBigInteger(uint256.NewInt((ic.Block.Timestamp))))
152152
return nil
153153
}
154154

155155
// BurnGas burns GAS to benefit Neo ecosystem.
156156
func BurnGas(ic *interop.Context) error {
157157
gas := ic.VM.Estack().Pop().BigInt()
158-
if !gas.IsInt64() {
158+
if !gas.IsUint64() {
159159
return errors.New("invalid GAS value")
160160
}
161161

162-
g := gas.Int64()
163-
if g <= 0 {
164-
return errors.New("GAS must be positive")
165-
}
162+
g := gas.Uint64()
166163

167-
if !ic.VM.AddGas(g) {
164+
if !ic.VM.AddGas(int64(g)) {
168165
return errors.New("GAS limit exceeded")
169166
}
170167
return nil

pkg/core/interop/runtime/ext_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"testing"
99

10+
"github.com/holiman/uint256"
1011
"github.com/nspcc-dev/neo-go/internal/contracts"
1112
"github.com/nspcc-dev/neo-go/internal/random"
1213
"github.com/nspcc-dev/neo-go/pkg/config"
@@ -491,7 +492,7 @@ func TestGetAddressVersion(t *testing.T) {
491492

492493
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetAddressVersion)
493494
require.NoError(t, w.Err)
494-
e.InvokeScriptCheckHALT(t, w.Bytes(), []neotest.Signer{acc}, stackitem.NewBigInteger(big.NewInt(int64(address.NEO3Prefix))))
495+
e.InvokeScriptCheckHALT(t, w.Bytes(), []neotest.Signer{acc}, stackitem.NewBigInteger(uint256.NewInt(uint64(address.NEO3Prefix))))
495496
}
496497

497498
func TestGetInvocationCounter(t *testing.T) {
@@ -506,19 +507,19 @@ func TestGetInvocationCounter(t *testing.T) {
506507
v.Load([]byte{1})
507508
// do not return an error in this case.
508509
require.NoError(t, runtime.GetInvocationCounter(ic))
509-
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Int64())
510+
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Uint64())
510511
})
511512
t.Run("NonZero", func(t *testing.T) {
512513
v.Load([]byte{2})
513514
require.NoError(t, runtime.GetInvocationCounter(ic))
514-
require.EqualValues(t, 42, v.Estack().Pop().BigInt().Int64())
515+
require.EqualValues(t, 42, v.Estack().Pop().BigInt().Uint64())
515516
})
516517
t.Run("Contract", func(t *testing.T) {
517518
script, err := smartcontract.CreateCallScript(cs.Hash, "invocCounter")
518519
require.NoError(t, err)
519520
v.LoadWithFlags(script, callflag.All)
520521
require.NoError(t, v.Run())
521-
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Int64())
522+
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Uint64())
522523
})
523524
}
524525

@@ -529,7 +530,7 @@ func TestGetNetwork(t *testing.T) {
529530

530531
emit.Syscall(w.BinWriter, interopnames.SystemRuntimeGetNetwork)
531532
require.NoError(t, w.Err)
532-
e.InvokeScriptCheckHALT(t, w.Bytes(), []neotest.Signer{acc}, stackitem.NewBigInteger(big.NewInt(int64(bc.GetConfig().Magic))))
533+
e.InvokeScriptCheckHALT(t, w.Bytes(), []neotest.Signer{acc}, stackitem.NewBigInteger(uint256.NewInt(uint64(bc.GetConfig().Magic))))
533534
}
534535

535536
func TestGetNotifications(t *testing.T) {

0 commit comments

Comments
 (0)