Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use uint256.Int in vm #2928

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli/nep_test/nep11_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"encoding/json"
"fmt"
"io"
"math/big"
"os"
"path/filepath"
"strconv"
"testing"

"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/internal/testcli"
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
"github.com/nspcc-dev/neo-go/pkg/core/state"
Expand Down Expand Up @@ -351,7 +351,7 @@ func TestNEP11_ND_OwnerOf_BalanceOf_Transfer(t *testing.T) {
Name: "OnNEP11Payment",
Item: stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray(nftOwnerHash.BytesBE()),
stackitem.NewBigInteger(big.NewInt(1)),
stackitem.NewBigInteger(uint256.NewInt(1)),
stackitem.NewByteArray(tokenID1),
stackitem.NewByteArray([]byte("some_data")),
}),
Expand Down Expand Up @@ -636,7 +636,7 @@ func TestNEP11_D_OwnerOf_BalanceOf_Transfer(t *testing.T) {
Name: "OnNEP11Payment",
Item: stackitem.NewArray([]stackitem.Item{
stackitem.NewByteArray(validatorHash.BytesBE()),
stackitem.NewBigInteger(big.NewInt(25)),
stackitem.NewBigInteger(uint256.NewInt(25)),
stackitem.NewByteArray(token2ID),
stackitem.NewByteArray([]byte("some_data")),
}),
Expand Down
8 changes: 4 additions & 4 deletions cli/vm/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"errors"
"fmt"
gio "io"
"math/big"
"os"
"path/filepath"
"strconv"
Expand All @@ -18,6 +17,7 @@ import (
"time"

"github.com/chzyer/readline"
"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/cli/paramcontext"
"github.com/nspcc-dev/neo-go/internal/basicchain"
"github.com/nspcc-dev/neo-go/internal/random"
Expand Down Expand Up @@ -590,9 +590,9 @@ func TestRunWithDifferentArguments(t *testing.T) {

e.checkNextLine(t, "READY: loaded \\d.* instructions")
e.checkStack(t, []stackitem.Item{
stackitem.NewBigInteger(big.NewInt(1)),
stackitem.NewBigInteger(big.NewInt(2)),
stackitem.NewBigInteger(big.NewInt(3)),
stackitem.NewBigInteger(uint256.NewInt(1)),
stackitem.NewBigInteger(uint256.NewInt(2)),
stackitem.NewBigInteger(uint256.NewInt(3)),
})
}

Expand Down
6 changes: 6 additions & 0 deletions go.work
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
go 1.20

use (
.
./pkg/interop
)
11 changes: 6 additions & 5 deletions pkg/compiler/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger"
"github.com/nspcc-dev/neo-go/pkg/vm"
Expand Down Expand Up @@ -36,7 +37,7 @@ func TestEntryPointWithArgs(t *testing.T) {
return 2 + args[1].(int)
}
`
args := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(0)), stackitem.NewBigInteger(big.NewInt(1))}
args := []stackitem.Item{stackitem.NewBigInteger(uint256.NewInt(0)), stackitem.NewBigInteger(uint256.NewInt(1))}
evalWithArgs(t, src, nil, args, big.NewInt(3))
}

Expand All @@ -51,7 +52,7 @@ func TestEntryPointWithMethodAndArgs(t *testing.T) {
return 0
}
`
args := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(0)), stackitem.NewBigInteger(big.NewInt(1))}
args := []stackitem.Item{stackitem.NewBigInteger(uint256.NewInt(0)), stackitem.NewBigInteger(uint256.NewInt(1))}
evalWithArgs(t, src, []byte("foobar"), args, big.NewInt(3))
}

Expand Down Expand Up @@ -156,9 +157,9 @@ func TestIntArray(t *testing.T) {
}
`
eval(t, src, []stackitem.Item{
stackitem.NewBigInteger(big.NewInt(1)),
stackitem.NewBigInteger(big.NewInt(2)),
stackitem.NewBigInteger(big.NewInt(3)),
stackitem.NewBigInteger(uint256.NewInt(1)),
stackitem.NewBigInteger(uint256.NewInt(2)),
stackitem.NewBigInteger(uint256.NewInt(3)),
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/compiler/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func TestForcedNotifyArgumentsConversion(t *testing.T) {
Name: strconv.Itoa(i),
Type: targetSCParamTypes[i],
}
defaultValue := stackitem.NewBigInteger(big.NewInt(int64(i)))
defaultValue := stackitem.NewBigIntegerFromInt64(int64(i))
var (
val stackitem.Item
err error
Expand Down
4 changes: 2 additions & 2 deletions pkg/compiler/native_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"errors"
"fmt"
"math/big"
"strconv"
"strings"
"testing"

"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/config"
"github.com/nspcc-dev/neo-go/pkg/core/interop"
Expand Down Expand Up @@ -362,7 +362,7 @@ func getTestStackItem(typ smartcontract.ParamType) stackitem.Item {
case smartcontract.BoolType:
return stackitem.NewBool(true)
case smartcontract.IntegerType:
return stackitem.NewBigInteger(big.NewInt(42))
return stackitem.NewBigInteger(uint256.NewInt(42))
case smartcontract.ByteArrayType, smartcontract.StringType, smartcontract.Hash160Type,
smartcontract.Hash256Type, smartcontract.PublicKeyType, smartcontract.SignatureType:
return stackitem.NewByteArray([]byte("result"))
Expand Down
11 changes: 6 additions & 5 deletions pkg/compiler/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
Expand Down Expand Up @@ -162,11 +163,11 @@ var sliceTestCases = []testCase{
}
`,
[]stackitem.Item{
stackitem.NewBigInteger(big.NewInt(1)),
stackitem.NewBigInteger(big.NewInt(2)),
stackitem.NewBigInteger(big.NewInt(3)),
stackitem.NewBigInteger(big.NewInt(4)),
stackitem.NewBigInteger(big.NewInt(5)),
stackitem.NewBigInteger(uint256.NewInt(1)),
stackitem.NewBigInteger(uint256.NewInt(2)),
stackitem.NewBigInteger(uint256.NewInt(3)),
stackitem.NewBigInteger(uint256.NewInt(4)),
stackitem.NewBigInteger(uint256.NewInt(5)),
},
},
{
Expand Down
5 changes: 3 additions & 2 deletions pkg/compiler/struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/pkg/compiler"
"github.com/nspcc-dev/neo-go/pkg/vm"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
Expand Down Expand Up @@ -278,8 +279,8 @@ var structTestCases = []testCase{
}
`,
[]stackitem.Item{
stackitem.NewBigInteger(big.NewInt(1)),
stackitem.NewBigInteger(big.NewInt(2)),
stackitem.NewBigInteger(uint256.NewInt(1)),
stackitem.NewBigInteger(uint256.NewInt(2)),
stackitem.NewByteArray([]byte("hello")),
stackitem.NewBool(false),
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/compiler/syscall_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

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

exp0 := []stackitem.Item{stackitem.NewBigInteger(big.NewInt(11)), stackitem.NewByteArray([]byte("sum")), stackitem.NewBigInteger(big.NewInt(12))}
exp0 := []stackitem.Item{stackitem.NewBigInteger(uint256.NewInt(11)), stackitem.NewByteArray([]byte("sum")), stackitem.NewBigInteger(uint256.NewInt(12))}
assert.Equal(t, "Event1", s.events[0].Name)
assert.Equal(t, exp0, s.events[0].Item.Value())
assert.Equal(t, "single", s.events[1].Name)
Expand Down
12 changes: 6 additions & 6 deletions pkg/core/block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"encoding/json"
"errors"
"math"
"math/big"

"github.com/holiman/uint256"
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
"github.com/nspcc-dev/neo-go/pkg/crypto/hash"
"github.com/nspcc-dev/neo-go/pkg/io"
Expand Down Expand Up @@ -223,14 +223,14 @@ func (b *Block) GetExpectedBlockSizeWithoutTransactions(txCount int) int {
func (b *Block) ToStackItem() stackitem.Item {
items := []stackitem.Item{
stackitem.NewByteArray(b.Hash().BytesBE()),
stackitem.NewBigInteger(big.NewInt(int64(b.Version))),
stackitem.NewBigInteger(uint256.NewInt(uint64(b.Version))),
stackitem.NewByteArray(b.PrevHash.BytesBE()),
stackitem.NewByteArray(b.MerkleRoot.BytesBE()),
stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))),
stackitem.NewBigInteger(new(big.Int).SetUint64(b.Nonce)),
stackitem.NewBigInteger(big.NewInt(int64(b.Index))),
stackitem.NewBigInteger(uint256.NewInt(uint64(b.Timestamp))),
stackitem.NewBigInteger(uint256.NewInt(b.Nonce)),
stackitem.NewBigInteger(uint256.NewInt(uint64(b.Index))),
stackitem.NewByteArray(b.NextConsensus.BytesBE()),
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
stackitem.NewBigInteger(uint256.NewInt(uint64(len(b.Transactions)))),
}
if b.StateRootEnabled {
items = append(items, stackitem.NewByteArray(b.PrevStateRoot.BytesBE()))
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,7 @@ func (bc *Blockchain) handleNotification(note *state.NotificationEvent, d *dao.S
return
}
}
bc.processTokenTransfer(d, transCache, h, b, note.ScriptHash, from, to, amount, id)
bc.processTokenTransfer(d, transCache, h, b, note.ScriptHash, from, to, util.ToBig(amount), id)
}

func parseUint160(itm stackitem.Item) (util.Uint160, error) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/core/interop/contract/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package contract
import (
"errors"
"fmt"
"math/big"
"strings"

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

// GetCallFlags returns current context calling flags.
func GetCallFlags(ic *interop.Context) error {
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(ic.VM.Context().GetCallFlags()))))
ic.VM.Estack().PushItem(stackitem.NewBigInteger(uint256.NewInt(uint64(ic.VM.Context().GetCallFlags()))))
return nil
}
3 changes: 2 additions & 1 deletion pkg/core/interop/contract/call_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"
"testing"

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

t.Run("good", func(t *testing.T) {
realBalance, _ := bc.GetGoverningTokenBalance(acc.ScriptHash())
cInvoker.Invoke(t, stackitem.NewBigInteger(big.NewInt(realBalance.Int64()+1)), "callT0", acc.ScriptHash())
cInvoker.Invoke(t, stackitem.NewBigInteger(uint256.NewInt(uint64(realBalance.Int64()+1))), "callT0", acc.ScriptHash())
})
t.Run("invalid param count", func(t *testing.T) {
cInvoker.InvokeFail(t, "method not found: callT2/1", "callT2", acc.ScriptHash())
Expand Down
2 changes: 1 addition & 1 deletion pkg/core/interop/iterator/interop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (t *testIter) Next() bool {
}

func (t testIter) Value() stackitem.Item {
return stackitem.NewBigInteger(big.NewInt(int64(t.arr[t.index])))
return stackitem.NewBigIntegerFromInt64(int64(t.arr[t.index]))
}

// Iterator is thoroughly tested in VM package, these are smoke tests.
Expand Down
17 changes: 7 additions & 10 deletions pkg/core/interop/runtime/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package runtime
import (
"errors"
"fmt"
"math/big"

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

// GetTrigger returns the script trigger.
func GetTrigger(ic *interop.Context) error {
ic.VM.Estack().PushItem(stackitem.NewBigInteger(big.NewInt(int64(ic.Trigger))))
ic.VM.Estack().PushItem(stackitem.NewBigInteger(uint256.NewInt(uint64(ic.Trigger))))
return nil
}

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

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

g := gas.Int64()
if g <= 0 {
return errors.New("GAS must be positive")
}
g := gas.Uint64()

if !ic.VM.AddGas(g) {
if !ic.VM.AddGas(int64(g)) {
return errors.New("GAS limit exceeded")
}
return nil
Expand Down
11 changes: 6 additions & 5 deletions pkg/core/interop/runtime/ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"testing"

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

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

func TestGetInvocationCounter(t *testing.T) {
Expand All @@ -506,19 +507,19 @@ func TestGetInvocationCounter(t *testing.T) {
v.Load([]byte{1})
// do not return an error in this case.
require.NoError(t, runtime.GetInvocationCounter(ic))
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Int64())
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Uint64())
})
t.Run("NonZero", func(t *testing.T) {
v.Load([]byte{2})
require.NoError(t, runtime.GetInvocationCounter(ic))
require.EqualValues(t, 42, v.Estack().Pop().BigInt().Int64())
require.EqualValues(t, 42, v.Estack().Pop().BigInt().Uint64())
})
t.Run("Contract", func(t *testing.T) {
script, err := smartcontract.CreateCallScript(cs.Hash, "invocCounter")
require.NoError(t, err)
v.LoadWithFlags(script, callflag.All)
require.NoError(t, v.Run())
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Int64())
require.EqualValues(t, 1, v.Estack().Pop().BigInt().Uint64())
})
}

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

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

func TestGetNotifications(t *testing.T) {
Expand Down
Loading