Merge pull request #2686 from nspcc-dev/drop-at-block-rpcs

rpcclient: simplify historic API
This commit is contained in:
Roman Khimov 2022-09-09 15:18:27 +03:00 committed by GitHub
commit 31792e3132
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 76 deletions

View file

@ -49,13 +49,10 @@ type RPCInvoke interface {
type RPCInvokeHistoric interface {
RPCSessions
InvokeContractVerifyAtBlock(blockHash util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error)
InvokeContractVerifyAtHeight(height uint32, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error)
InvokeContractVerifyWithState(stateroot util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error)
InvokeFunctionAtBlock(blockHash util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error)
InvokeFunctionAtHeight(height uint32, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error)
InvokeFunctionWithState(stateroot util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error)
InvokeScriptAtBlock(blockHash util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error)
InvokeScriptAtHeight(height uint32, script []byte, signers []transaction.Signer) (*result.Invoke, error)
InvokeScriptWithState(stateroot util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error)
}
@ -73,7 +70,6 @@ type Invoker struct {
type historicConverter struct {
client RPCInvokeHistoric
block *util.Uint256
height *uint32
root *util.Uint256
}
@ -86,14 +82,6 @@ func New(client RPCInvoke, signers []transaction.Signer) *Invoker {
return &Invoker{client, signers}
}
// NewHistoricAtBlock creates an Invoker to test-execute things at some given block.
func NewHistoricAtBlock(block util.Uint256, client RPCInvokeHistoric, signers []transaction.Signer) *Invoker {
return New(&historicConverter{
client: client,
block: &block,
}, signers)
}
// NewHistoricAtHeight creates an Invoker to test-execute things at some given height.
func NewHistoricAtHeight(height uint32, client RPCInvokeHistoric, signers []transaction.Signer) *Invoker {
return New(&historicConverter{
@ -102,18 +90,16 @@ func NewHistoricAtHeight(height uint32, client RPCInvokeHistoric, signers []tran
}, signers)
}
// NewHistoricWithState creates an Invoker to test-execute things with some given state.
func NewHistoricWithState(root util.Uint256, client RPCInvokeHistoric, signers []transaction.Signer) *Invoker {
// NewHistoricWithState creates an Invoker to test-execute things with some
// given state or block.
func NewHistoricWithState(rootOrBlock util.Uint256, client RPCInvokeHistoric, signers []transaction.Signer) *Invoker {
return New(&historicConverter{
client: client,
root: &root,
root: &rootOrBlock,
}, signers)
}
func (h *historicConverter) InvokeScript(script []byte, signers []transaction.Signer) (*result.Invoke, error) {
if h.block != nil {
return h.client.InvokeScriptAtBlock(*h.block, script, signers)
}
if h.height != nil {
return h.client.InvokeScriptAtHeight(*h.height, script, signers)
}
@ -124,9 +110,6 @@ func (h *historicConverter) InvokeScript(script []byte, signers []transaction.Si
}
func (h *historicConverter) InvokeFunction(contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
if h.block != nil {
return h.client.InvokeFunctionAtBlock(*h.block, contract, operation, params, signers)
}
if h.height != nil {
return h.client.InvokeFunctionAtHeight(*h.height, contract, operation, params, signers)
}
@ -137,9 +120,6 @@ func (h *historicConverter) InvokeFunction(contract util.Uint160, operation stri
}
func (h *historicConverter) InvokeContractVerify(contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
if h.block != nil {
return h.client.InvokeContractVerifyAtBlock(*h.block, contract, params, signers, witnesses...)
}
if h.height != nil {
return h.client.InvokeContractVerifyAtHeight(*h.height, contract, params, signers, witnesses...)
}

View file

@ -29,27 +29,18 @@ func (r *rpcInv) InvokeFunction(contract util.Uint160, operation string, params
func (r *rpcInv) InvokeScript(script []byte, signers []transaction.Signer) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeContractVerifyAtBlock(blockHash util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeContractVerifyAtHeight(height uint32, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeContractVerifyWithState(stateroot util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeFunctionAtBlock(blockHash util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeFunctionAtHeight(height uint32, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeFunctionWithState(stateroot util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeScriptAtBlock(blockHash util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
return r.resInv, r.err
}
func (r *rpcInv) InvokeScriptAtHeight(height uint32, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
return r.resInv, r.err
}
@ -108,9 +99,6 @@ func TestInvoker(t *testing.T) {
t.Run("standard", func(t *testing.T) {
testInv(t, New(ri, nil))
})
t.Run("historic, block", func(t *testing.T) {
testInv(t, NewHistoricAtBlock(util.Uint256{}, ri, nil))
})
t.Run("historic, height", func(t *testing.T) {
testInv(t, NewHistoricAtHeight(100500, ri, nil))
})
@ -124,7 +112,7 @@ func TestInvoker(t *testing.T) {
require.Panics(t, func() { _, _ = inv.Run([]byte{1}) })
})
t.Run("terminate session", func(t *testing.T) {
for _, inv := range []*Invoker{New(ri, nil), NewHistoricAtBlock(util.Uint256{}, ri, nil)} {
for _, inv := range []*Invoker{New(ri, nil), NewHistoricWithState(util.Uint256{}, ri, nil)} {
ri.err = errors.New("")
require.Error(t, inv.TerminateSession(uuid.UUID{}))
ri.err = nil
@ -135,7 +123,7 @@ func TestInvoker(t *testing.T) {
}
})
t.Run("traverse iterator", func(t *testing.T) {
for _, inv := range []*Invoker{New(ri, nil), NewHistoricAtBlock(util.Uint256{}, ri, nil)} {
for _, inv := range []*Invoker{New(ri, nil), NewHistoricWithState(util.Uint256{}, ri, nil)} {
res, err := inv.TraverseIterator(uuid.UUID{}, &result.Iterator{
Values: []stackitem.Item{stackitem.Make(42)},
}, 0)

View file

@ -605,21 +605,12 @@ func (c *Client) InvokeScriptAtHeight(height uint32, script []byte, signers []tr
return c.invokeSomething("invokescripthistoric", p, signers)
}
// InvokeScriptAtBlock returns the result of the given script after running it
// true the VM using the provided chain state retrieved from the specified block
// hash.
// NOTE: This is a test invoke and will not affect the blockchain.
func (c *Client) InvokeScriptAtBlock(blockHash util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
var p = []interface{}{blockHash.StringLE(), script}
return c.invokeSomething("invokescripthistoric", p, signers)
}
// InvokeScriptWithState returns the result of the given script after running it
// true the VM using the provided chain state retrieved from the specified
// stateroot hash.
// state root or block hash.
// NOTE: This is a test invoke and will not affect the blockchain.
func (c *Client) InvokeScriptWithState(stateroot util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
var p = []interface{}{stateroot.StringLE(), script}
func (c *Client) InvokeScriptWithState(stateOrBlock util.Uint256, script []byte, signers []transaction.Signer) (*result.Invoke, error) {
var p = []interface{}{stateOrBlock.StringLE(), script}
return c.invokeSomething("invokescripthistoric", p, signers)
}
@ -640,21 +631,12 @@ func (c *Client) InvokeFunctionAtHeight(height uint32, contract util.Uint160, op
return c.invokeSomething("invokefunctionhistoric", p, signers)
}
// InvokeFunctionAtBlock returns the results after calling the smart contract
// with the given operation and parameters at given the blockchain state
// specified by the block hash.
// NOTE: this is test invoke and will not affect the blockchain.
func (c *Client) InvokeFunctionAtBlock(blockHash util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
var p = []interface{}{blockHash.StringLE(), contract.StringLE(), operation, params}
return c.invokeSomething("invokefunctionhistoric", p, signers)
}
// InvokeFunctionWithState returns the results after calling the smart contract
// with the given operation and parameters at the given blockchain state defined
// by the specified stateroot hash.
// by the specified state root or block hash.
// NOTE: this is test invoke and will not affect the blockchain.
func (c *Client) InvokeFunctionWithState(stateroot util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
var p = []interface{}{stateroot.StringLE(), contract.StringLE(), operation, params}
func (c *Client) InvokeFunctionWithState(stateOrBlock util.Uint256, contract util.Uint160, operation string, params []smartcontract.Parameter, signers []transaction.Signer) (*result.Invoke, error) {
var p = []interface{}{stateOrBlock.StringLE(), contract.StringLE(), operation, params}
return c.invokeSomething("invokefunctionhistoric", p, signers)
}
@ -675,21 +657,12 @@ func (c *Client) InvokeContractVerifyAtHeight(height uint32, contract util.Uint1
return c.invokeSomething("invokecontractverifyhistoric", p, signers, witnesses...)
}
// InvokeContractVerifyAtBlock returns the results after calling `verify` method
// of the smart contract with the given parameters under verification trigger type
// at the blockchain state specified by the block hash.
// NOTE: this is test invoke and will not affect the blockchain.
func (c *Client) InvokeContractVerifyAtBlock(blockHash util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
var p = []interface{}{blockHash.StringLE(), contract.StringLE(), params}
return c.invokeSomething("invokecontractverifyhistoric", p, signers, witnesses...)
}
// InvokeContractVerifyWithState returns the results after calling `verify` method
// of the smart contract with the given parameters under verification trigger type
// at the blockchain state specified by the stateroot hash.
// at the blockchain state specified by the state root or block hash.
// NOTE: this is test invoke and will not affect the blockchain.
func (c *Client) InvokeContractVerifyWithState(stateroot util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
var p = []interface{}{stateroot.StringLE(), contract.StringLE(), params}
func (c *Client) InvokeContractVerifyWithState(stateOrBlock util.Uint256, contract util.Uint160, params []smartcontract.Parameter, signers []transaction.Signer, witnesses ...transaction.Witness) (*result.Invoke, error) {
var p = []interface{}{stateOrBlock.StringLE(), contract.StringLE(), params}
return c.invokeSomething("invokecontractverifyhistoric", p, signers, witnesses...)
}

View file

@ -1264,7 +1264,7 @@ func TestInvokeVerify(t *testing.T) {
})
t.Run("positive, historic, by block, with signer", func(t *testing.T) {
res, err := c.InvokeContractVerifyAtBlock(chain.GetHeaderHash(int(chain.BlockHeight())-1), contract, []smartcontract.Parameter{}, []transaction.Signer{{Account: testchain.PrivateKeyByID(0).PublicKey().GetScriptHash()}})
res, err := c.InvokeContractVerifyWithState(chain.GetHeaderHash(int(chain.BlockHeight())-1), contract, []smartcontract.Parameter{}, []transaction.Signer{{Account: testchain.PrivateKeyByID(0).PublicKey().GetScriptHash()}})
require.NoError(t, err)
require.Equal(t, "HALT", res.State)
require.Equal(t, 1, len(res.Stack))
@ -1290,7 +1290,7 @@ func TestInvokeVerify(t *testing.T) {
})
t.Run("bad, historic, by block: contract not found", func(t *testing.T) {
_, err = c.InvokeContractVerifyAtBlock(chain.GetHeaderHash(1), contract, []smartcontract.Parameter{}, []transaction.Signer{{Account: testchain.PrivateKeyByID(0).PublicKey().GetScriptHash()}})
_, err = c.InvokeContractVerifyWithState(chain.GetHeaderHash(1), contract, []smartcontract.Parameter{}, []transaction.Signer{{Account: testchain.PrivateKeyByID(0).PublicKey().GetScriptHash()}})
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), core.ErrUnknownVerificationContract.Error())) // contract wasn't deployed at block #1 yet
})