core: simplify newInteropContext
This commit is contained in:
parent
45a4524054
commit
637c99eda7
3 changed files with 16 additions and 14 deletions
|
@ -508,7 +508,7 @@ func (bc *Blockchain) storeBlock(block *Block) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case *transaction.InvocationTX:
|
case *transaction.InvocationTX:
|
||||||
systemInterop := newInteropContext(trigger.Application, bc, cache.store, block, tx, bc.log)
|
systemInterop := bc.newInteropContext(trigger.Application, cache.store, block, tx)
|
||||||
v := bc.spawnVMWithInterops(systemInterop)
|
v := bc.spawnVMWithInterops(systemInterop)
|
||||||
v.SetCheckedHash(tx.VerificationHash().BytesBE())
|
v.SetCheckedHash(tx.VerificationHash().BytesBE())
|
||||||
v.LoadScript(t.Script)
|
v.LoadScript(t.Script)
|
||||||
|
@ -1373,7 +1373,7 @@ func (bc *Blockchain) spawnVMWithInterops(interopCtx *interopContext) *vm.VM {
|
||||||
// GetTestVM returns a VM and a Store setup for a test run of some sort of code.
|
// GetTestVM returns a VM and a Store setup for a test run of some sort of code.
|
||||||
func (bc *Blockchain) GetTestVM() (*vm.VM, storage.Store) {
|
func (bc *Blockchain) GetTestVM() (*vm.VM, storage.Store) {
|
||||||
tmpStore := storage.NewMemCachedStore(bc.dao.store)
|
tmpStore := storage.NewMemCachedStore(bc.dao.store)
|
||||||
systemInterop := newInteropContext(trigger.Application, bc, tmpStore, nil, nil, bc.log)
|
systemInterop := bc.newInteropContext(trigger.Application, tmpStore, nil, nil)
|
||||||
vm := bc.spawnVMWithInterops(systemInterop)
|
vm := bc.spawnVMWithInterops(systemInterop)
|
||||||
return vm, tmpStore
|
return vm, tmpStore
|
||||||
}
|
}
|
||||||
|
@ -1452,7 +1452,7 @@ func (bc *Blockchain) verifyTxWitnesses(t *transaction.Transaction, block *Block
|
||||||
}
|
}
|
||||||
sort.Slice(hashes, func(i, j int) bool { return hashes[i].Less(hashes[j]) })
|
sort.Slice(hashes, func(i, j int) bool { return hashes[i].Less(hashes[j]) })
|
||||||
sort.Slice(witnesses, func(i, j int) bool { return witnesses[i].ScriptHash().Less(witnesses[j].ScriptHash()) })
|
sort.Slice(witnesses, func(i, j int) bool { return witnesses[i].ScriptHash().Less(witnesses[j].ScriptHash()) })
|
||||||
interopCtx := newInteropContext(trigger.Verification, bc, bc.dao.store, block, t, bc.log)
|
interopCtx := bc.newInteropContext(trigger.Verification, bc.dao.store, block, t)
|
||||||
for i := 0; i < len(hashes); i++ {
|
for i := 0; i < len(hashes); i++ {
|
||||||
err := bc.verifyHashAgainstScript(hashes[i], &witnesses[i], t.VerificationHash(), interopCtx, false)
|
err := bc.verifyHashAgainstScript(hashes[i], &witnesses[i], t.VerificationHash(), interopCtx, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1472,7 +1472,7 @@ func (bc *Blockchain) verifyBlockWitnesses(block *Block, prevHeader *Header) err
|
||||||
} else {
|
} else {
|
||||||
hash = prevHeader.NextConsensus
|
hash = prevHeader.NextConsensus
|
||||||
}
|
}
|
||||||
interopCtx := newInteropContext(trigger.Verification, bc, bc.dao.store, nil, nil, bc.log)
|
interopCtx := bc.newInteropContext(trigger.Verification, bc.dao.store, nil, nil)
|
||||||
return bc.verifyHashAgainstScript(hash, &block.Script, block.VerificationHash(), interopCtx, true)
|
return bc.verifyHashAgainstScript(hash, &block.Script, block.VerificationHash(), interopCtx, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1486,3 +1486,7 @@ func hashAndIndexToBytes(h util.Uint256, index uint32) []byte {
|
||||||
func (bc *Blockchain) secondsPerBlock() int {
|
func (bc *Blockchain) secondsPerBlock() int {
|
||||||
return bc.config.SecondsPerBlock
|
return bc.config.SecondsPerBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bc *Blockchain) newInteropContext(trigger byte, s storage.Store, block *Block, tx *transaction.Transaction) *interopContext {
|
||||||
|
return newInteropContext(trigger, bc, s, block, tx, bc.log)
|
||||||
|
}
|
||||||
|
|
|
@ -15,7 +15,6 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm"
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
"github.com/CityOfZion/neo-go/pkg/vm/opcode"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/zap/zaptest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
/* Missing tests:
|
/* Missing tests:
|
||||||
|
@ -113,7 +112,7 @@ func TestHeaderGetVersion(t *testing.T) {
|
||||||
func TestHeaderGetVersion_Negative(t *testing.T) {
|
func TestHeaderGetVersion_Negative(t *testing.T) {
|
||||||
v := vm.New()
|
v := vm.New()
|
||||||
block := newDumbBlock()
|
block := newDumbBlock()
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), block, nil, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), block, nil)
|
||||||
v.Estack().PushVal(vm.NewBoolItem(false))
|
v.Estack().PushVal(vm.NewBoolItem(false))
|
||||||
|
|
||||||
err := context.headerGetVersion(v)
|
err := context.headerGetVersion(v)
|
||||||
|
@ -198,7 +197,7 @@ func TestWitnessGetVerificationScript(t *testing.T) {
|
||||||
script := []byte{byte(opcode.PUSHM1), byte(opcode.RET)}
|
script := []byte{byte(opcode.PUSHM1), byte(opcode.RET)}
|
||||||
witness := transaction.Witness{InvocationScript: nil, VerificationScript: script}
|
witness := transaction.Witness{InvocationScript: nil, VerificationScript: script}
|
||||||
|
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), nil, nil)
|
||||||
v.Estack().PushVal(vm.NewInteropItem(&witness))
|
v.Estack().PushVal(vm.NewInteropItem(&witness))
|
||||||
err := context.witnessGetVerificationScript(v)
|
err := context.witnessGetVerificationScript(v)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -419,7 +418,7 @@ func TestAssetGetPrecision(t *testing.T) {
|
||||||
func createVMAndPushBlock(t *testing.T) (*vm.VM, *Block, *interopContext) {
|
func createVMAndPushBlock(t *testing.T) (*vm.VM, *Block, *interopContext) {
|
||||||
v := vm.New()
|
v := vm.New()
|
||||||
block := newDumbBlock()
|
block := newDumbBlock()
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), block, nil, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), block, nil)
|
||||||
v.Estack().PushVal(vm.NewInteropItem(block))
|
v.Estack().PushVal(vm.NewInteropItem(block))
|
||||||
return v, block, context
|
return v, block, context
|
||||||
}
|
}
|
||||||
|
@ -448,7 +447,7 @@ func createVMAndAssetState(t *testing.T) (*vm.VM, *state.Asset, *interopContext)
|
||||||
IsFrozen: false,
|
IsFrozen: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), nil, nil)
|
||||||
return v, assetState, context
|
return v, assetState, context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -466,7 +465,7 @@ func createVMAndContractState(t *testing.T) (*vm.VM, *state.Contract, *interopCo
|
||||||
Description: random.String(10),
|
Description: random.String(10),
|
||||||
}
|
}
|
||||||
|
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), nil, nil)
|
||||||
return v, contractState, context
|
return v, contractState, context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -480,7 +479,7 @@ func createVMAndAccState(t *testing.T) (*vm.VM, *state.Account, *interopContext)
|
||||||
accountState.Votes = []*keys.PublicKey{key}
|
accountState.Votes = []*keys.PublicKey{key}
|
||||||
|
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), nil, nil)
|
||||||
return v, accountState, context
|
return v, accountState, context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -510,6 +509,6 @@ func createVMAndTX(t *testing.T) (*vm.VM, *transaction.Transaction, *interopCont
|
||||||
tx.Attributes = attributes
|
tx.Attributes = attributes
|
||||||
tx.Inputs = inputs
|
tx.Inputs = inputs
|
||||||
tx.Outputs = outputs
|
tx.Outputs = outputs
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, tx, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), nil, tx)
|
||||||
return v, tx, context
|
return v, tx, context
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,13 +9,12 @@ import (
|
||||||
"github.com/CityOfZion/neo-go/pkg/smartcontract/trigger"
|
"github.com/CityOfZion/neo-go/pkg/smartcontract/trigger"
|
||||||
"github.com/CityOfZion/neo-go/pkg/vm"
|
"github.com/CityOfZion/neo-go/pkg/vm"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"go.uber.org/zap/zaptest"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func testNonInterop(t *testing.T, value interface{}, f func(*interopContext, *vm.VM) error) {
|
func testNonInterop(t *testing.T, value interface{}, f func(*interopContext, *vm.VM) error) {
|
||||||
v := vm.New()
|
v := vm.New()
|
||||||
v.Estack().PushVal(value)
|
v.Estack().PushVal(value)
|
||||||
context := newInteropContext(trigger.Application, newTestChain(t), storage.NewMemoryStore(), nil, nil, zaptest.NewLogger(t))
|
context := newTestChain(t).newInteropContext(trigger.Application, storage.NewMemoryStore(), nil, nil)
|
||||||
require.Error(t, f(context, v))
|
require.Error(t, f(context, v))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue