2021-02-03 19:01:20 +00:00
|
|
|
package native
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/big"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/block"
|
|
|
|
"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"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
2022-04-21 16:19:24 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
2021-02-03 19:01:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2022-04-04 10:22:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm"
|
2021-02-03 19:01:20 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ledger provides an interface to blocks/transactions storage for smart
|
|
|
|
// contracts. It's not a part of the proper chain's state, so it's just a
|
|
|
|
// proxy between regular Blockchain/DAO interface and smart contracts.
|
|
|
|
type Ledger struct {
|
|
|
|
interop.ContractMD
|
|
|
|
}
|
|
|
|
|
2021-05-12 15:13:14 +00:00
|
|
|
const ledgerContractID = -4
|
2021-02-03 19:01:20 +00:00
|
|
|
|
|
|
|
// newLedger creates new Ledger native contract.
|
|
|
|
func newLedger() *Ledger {
|
|
|
|
var l = &Ledger{
|
|
|
|
ContractMD: *interop.NewContractMD(nativenames.Ledger, ledgerContractID),
|
|
|
|
}
|
2021-02-15 13:40:44 +00:00
|
|
|
defer l.UpdateHash()
|
|
|
|
|
2021-02-03 19:01:20 +00:00
|
|
|
desc := newDescriptor("currentHash", smartcontract.Hash256Type)
|
2021-03-05 10:30:16 +00:00
|
|
|
md := newMethodAndPrice(l.currentHash, 1<<15, callflag.ReadStates)
|
2021-02-03 19:01:20 +00:00
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("currentIndex", smartcontract.IntegerType)
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(l.currentIndex, 1<<15, callflag.ReadStates)
|
2021-02-03 19:01:20 +00:00
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("getBlock", smartcontract.ArrayType,
|
|
|
|
manifest.NewParameter("indexOrHash", smartcontract.ByteArrayType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(l.getBlock, 1<<15, callflag.ReadStates)
|
2021-02-03 19:01:20 +00:00
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("getTransaction", smartcontract.ArrayType,
|
2021-02-12 13:53:23 +00:00
|
|
|
manifest.NewParameter("hash", smartcontract.Hash256Type))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(l.getTransaction, 1<<15, callflag.ReadStates)
|
2021-02-03 19:01:20 +00:00
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("getTransactionHeight", smartcontract.IntegerType,
|
2021-02-12 13:53:23 +00:00
|
|
|
manifest.NewParameter("hash", smartcontract.Hash256Type))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(l.getTransactionHeight, 1<<15, callflag.ReadStates)
|
2021-02-03 19:01:20 +00:00
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
|
|
|
desc = newDescriptor("getTransactionFromBlock", smartcontract.ArrayType,
|
|
|
|
manifest.NewParameter("blockIndexOrHash", smartcontract.ByteArrayType),
|
|
|
|
manifest.NewParameter("txIndex", smartcontract.IntegerType))
|
2021-03-05 10:30:16 +00:00
|
|
|
md = newMethodAndPrice(l.getTransactionFromBlock, 1<<16, callflag.ReadStates)
|
2021-02-03 19:01:20 +00:00
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
2022-04-21 16:19:24 +00:00
|
|
|
desc = newDescriptor("getTransactionSigners", smartcontract.ArrayType,
|
|
|
|
manifest.NewParameter("hash", smartcontract.Hash256Type))
|
|
|
|
md = newMethodAndPrice(l.getTransactionSigners, 1<<15, callflag.ReadStates)
|
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
2022-04-04 10:22:20 +00:00
|
|
|
desc = newDescriptor("getTransactionVMState", smartcontract.IntegerType,
|
|
|
|
manifest.NewParameter("hash", smartcontract.Hash256Type))
|
|
|
|
md = newMethodAndPrice(l.getTransactionVMState, 1<<15, callflag.ReadStates)
|
|
|
|
l.AddMethod(md, desc)
|
|
|
|
|
2021-02-03 19:01:20 +00:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
// Metadata implements Contract interface.
|
|
|
|
func (l *Ledger) Metadata() *interop.ContractMD {
|
|
|
|
return &l.ContractMD
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize implements Contract interface.
|
|
|
|
func (l *Ledger) Initialize(ic *interop.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnPersist implements Contract interface.
|
|
|
|
func (l *Ledger) OnPersist(ic *interop.Context) error {
|
|
|
|
// Actual block/tx processing is done in Blockchain.storeBlock().
|
|
|
|
// Even though C# node add them to storage here, they're not
|
|
|
|
// accessible to smart contracts (see isTraceableBlock()), thus
|
|
|
|
// the end effect is the same.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostPersist implements Contract interface.
|
|
|
|
func (l *Ledger) PostPersist(ic *interop.Context) error {
|
|
|
|
return nil // Actual block/tx processing is done in Blockchain.storeBlock().
|
|
|
|
}
|
|
|
|
|
|
|
|
// currentHash implements currentHash SC method.
|
|
|
|
func (l *Ledger) currentHash(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2022-04-29 15:00:46 +00:00
|
|
|
return stackitem.Make(ic.CurrentBlockHash().BytesBE())
|
2021-02-03 19:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// currentIndex implements currentIndex SC method.
|
|
|
|
func (l *Ledger) currentIndex(ic *interop.Context, _ []stackitem.Item) stackitem.Item {
|
2022-04-29 15:00:46 +00:00
|
|
|
return stackitem.Make(ic.BlockHeight())
|
2021-02-03 19:01:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getBlock implements getBlock SC method.
|
|
|
|
func (l *Ledger) getBlock(ic *interop.Context, params []stackitem.Item) stackitem.Item {
|
2022-04-29 15:00:46 +00:00
|
|
|
hash := getBlockHashFromItem(ic, params[0])
|
|
|
|
block, err := ic.GetBlock(hash)
|
|
|
|
if err != nil || !isTraceableBlock(ic, block.Index) {
|
2021-02-03 19:01:20 +00:00
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
return BlockToStackItem(block)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTransaction returns transaction to the SC.
|
|
|
|
func (l *Ledger) getTransaction(ic *interop.Context, params []stackitem.Item) stackitem.Item {
|
|
|
|
tx, h, err := getTransactionAndHeight(ic.DAO, params[0])
|
2022-04-29 15:00:46 +00:00
|
|
|
if err != nil || !isTraceableBlock(ic, h) {
|
2021-02-03 19:01:20 +00:00
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
return TransactionToStackItem(tx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTransactionHeight returns transaction height to the SC.
|
|
|
|
func (l *Ledger) getTransactionHeight(ic *interop.Context, params []stackitem.Item) stackitem.Item {
|
|
|
|
_, h, err := getTransactionAndHeight(ic.DAO, params[0])
|
2022-04-29 15:00:46 +00:00
|
|
|
if err != nil || !isTraceableBlock(ic, h) {
|
2021-02-03 19:01:20 +00:00
|
|
|
return stackitem.Make(-1)
|
|
|
|
}
|
|
|
|
return stackitem.Make(h)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTransactionFromBlock returns transaction with the given index from the
|
|
|
|
// block with height or hash specified.
|
|
|
|
func (l *Ledger) getTransactionFromBlock(ic *interop.Context, params []stackitem.Item) stackitem.Item {
|
2022-04-29 15:00:46 +00:00
|
|
|
hash := getBlockHashFromItem(ic, params[0])
|
2021-02-03 19:01:20 +00:00
|
|
|
index := toUint32(params[1])
|
2022-04-29 15:00:46 +00:00
|
|
|
block, err := ic.GetBlock(hash)
|
|
|
|
if err != nil || !isTraceableBlock(ic, block.Index) {
|
2021-02-03 19:01:20 +00:00
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
if index >= uint32(len(block.Transactions)) {
|
|
|
|
panic("wrong transaction index")
|
|
|
|
}
|
|
|
|
return TransactionToStackItem(block.Transactions[index])
|
|
|
|
}
|
|
|
|
|
2022-04-21 16:19:24 +00:00
|
|
|
// getTransactionSigners returns transaction signers to the SC.
|
|
|
|
func (l *Ledger) getTransactionSigners(ic *interop.Context, params []stackitem.Item) stackitem.Item {
|
|
|
|
tx, h, err := getTransactionAndHeight(ic.DAO, params[0])
|
2022-04-29 15:00:46 +00:00
|
|
|
if err != nil || !isTraceableBlock(ic, h) {
|
2022-04-21 16:19:24 +00:00
|
|
|
return stackitem.Null{}
|
|
|
|
}
|
|
|
|
return SignersToStackItem(tx.Signers)
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:22:20 +00:00
|
|
|
// getTransactionVMState returns VM state got after transaction invocation.
|
|
|
|
func (l *Ledger) getTransactionVMState(ic *interop.Context, params []stackitem.Item) stackitem.Item {
|
|
|
|
hash, err := getUint256FromItem(params[0])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
h, _, aer, err := ic.DAO.GetTxExecResult(hash)
|
2022-04-29 15:00:46 +00:00
|
|
|
if err != nil || !isTraceableBlock(ic, h) {
|
2022-04-04 10:22:20 +00:00
|
|
|
return stackitem.Make(vm.NoneState)
|
|
|
|
}
|
|
|
|
return stackitem.Make(aer.VMState)
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:01:20 +00:00
|
|
|
// isTraceableBlock defines whether we're able to give information about
|
|
|
|
// the block with index specified.
|
2022-04-29 15:00:46 +00:00
|
|
|
func isTraceableBlock(ic *interop.Context, index uint32) bool {
|
|
|
|
height := ic.BlockHeight()
|
|
|
|
MaxTraceableBlocks := ic.Chain.GetConfig().MaxTraceableBlocks
|
2021-02-03 19:01:20 +00:00
|
|
|
return index <= height && index+MaxTraceableBlocks > height
|
|
|
|
}
|
|
|
|
|
|
|
|
// getBlockHashFromItem converts given stackitem.Item to block hash using given
|
2022-01-12 22:08:53 +00:00
|
|
|
// Ledger if needed. Interop functions accept both block numbers and
|
2021-02-03 19:01:20 +00:00
|
|
|
// block hashes as parameters, thus this function is needed. It's supposed to
|
|
|
|
// be called within VM context, so it panics if anything goes wrong.
|
2022-04-29 15:00:46 +00:00
|
|
|
func getBlockHashFromItem(ic *interop.Context, item stackitem.Item) util.Uint256 {
|
2021-02-03 19:01:20 +00:00
|
|
|
bigindex, err := item.TryInteger()
|
2021-07-19 12:15:14 +00:00
|
|
|
if err == nil && bigindex.IsUint64() {
|
|
|
|
index := bigindex.Uint64()
|
|
|
|
if index > math.MaxUint32 {
|
2021-02-03 19:01:20 +00:00
|
|
|
panic("bad block index")
|
|
|
|
}
|
2022-04-29 15:00:46 +00:00
|
|
|
if uint32(index) > ic.BlockHeight() {
|
2021-02-03 19:01:20 +00:00
|
|
|
panic(fmt.Errorf("no block with index %d", index))
|
|
|
|
}
|
2022-04-29 15:00:46 +00:00
|
|
|
return ic.Chain.GetHeaderHash(int(index))
|
2021-02-03 19:01:20 +00:00
|
|
|
}
|
2022-04-04 09:33:29 +00:00
|
|
|
hash, err := getUint256FromItem(item)
|
2021-02-03 19:01:20 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return hash
|
|
|
|
}
|
|
|
|
|
2022-04-04 09:33:29 +00:00
|
|
|
func getUint256FromItem(item stackitem.Item) (util.Uint256, error) {
|
2021-02-03 19:01:20 +00:00
|
|
|
hashbytes, err := item.TryBytes()
|
|
|
|
if err != nil {
|
2022-04-04 09:33:29 +00:00
|
|
|
return util.Uint256{}, fmt.Errorf("failed to get hash bytes: %w", err)
|
2021-02-03 19:01:20 +00:00
|
|
|
}
|
|
|
|
hash, err := util.Uint256DecodeBytesBE(hashbytes)
|
2022-04-04 09:33:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return util.Uint256{}, fmt.Errorf("failed to decode hash: %w", err)
|
|
|
|
}
|
|
|
|
return hash, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTransactionAndHeight returns transaction and its height if it's present
|
|
|
|
// on the chain. It panics if anything goes wrong.
|
|
|
|
func getTransactionAndHeight(d *dao.Simple, item stackitem.Item) (*transaction.Transaction, uint32, error) {
|
|
|
|
hash, err := getUint256FromItem(item)
|
2021-02-03 19:01:20 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-07-29 19:33:53 +00:00
|
|
|
return d.GetTransaction(hash)
|
2021-02-03 19:01:20 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// BlockToStackItem converts block.Block to stackitem.Item.
|
2021-02-03 19:01:20 +00:00
|
|
|
func BlockToStackItem(b *block.Block) stackitem.Item {
|
|
|
|
return stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(b.Hash().BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Version))),
|
|
|
|
stackitem.NewByteArray(b.PrevHash.BytesBE()),
|
|
|
|
stackitem.NewByteArray(b.MerkleRoot.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))),
|
2021-07-14 08:32:54 +00:00
|
|
|
stackitem.NewBigInteger(new(big.Int).SetUint64(b.Nonce)),
|
2021-02-03 19:01:20 +00:00
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(b.Index))),
|
|
|
|
stackitem.NewByteArray(b.NextConsensus.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-12 20:17:03 +00:00
|
|
|
// TransactionToStackItem converts transaction.Transaction to stackitem.Item.
|
2021-02-03 19:01:20 +00:00
|
|
|
func TransactionToStackItem(t *transaction.Transaction) stackitem.Item {
|
|
|
|
return stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(t.Hash().BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.Version))),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.Nonce))),
|
|
|
|
stackitem.NewByteArray(t.Sender().BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.SystemFee))),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.NetworkFee))),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(t.ValidUntilBlock))),
|
|
|
|
stackitem.NewByteArray(t.Script),
|
|
|
|
})
|
|
|
|
}
|
2022-04-21 16:19:24 +00:00
|
|
|
|
|
|
|
// SignersToStackItem converts transaction.Signers to stackitem.Item.
|
|
|
|
func SignersToStackItem(signers []transaction.Signer) stackitem.Item {
|
|
|
|
res := make([]stackitem.Item, len(signers))
|
|
|
|
bw := io.NewBufBinWriter()
|
|
|
|
for i, s := range signers {
|
|
|
|
s.EncodeBinary(bw.BinWriter)
|
|
|
|
if bw.Err != nil {
|
|
|
|
panic(fmt.Errorf("failed to serialize signer %d to stackitem: %w", i, bw.Err))
|
|
|
|
}
|
|
|
|
contracts := make([]stackitem.Item, len(s.AllowedContracts))
|
|
|
|
for j, c := range s.AllowedContracts {
|
|
|
|
contracts[j] = stackitem.NewByteArray(c.BytesBE())
|
|
|
|
}
|
|
|
|
groups := make([]stackitem.Item, len(s.AllowedGroups))
|
|
|
|
for j, g := range s.AllowedGroups {
|
|
|
|
groups[j] = stackitem.NewByteArray(g.Bytes())
|
|
|
|
}
|
|
|
|
rules := make([]stackitem.Item, len(s.Rules))
|
|
|
|
for j, r := range s.Rules {
|
|
|
|
rules[j] = r.ToStackItem()
|
|
|
|
}
|
|
|
|
res[i] = stackitem.NewArray([]stackitem.Item{
|
|
|
|
stackitem.NewByteArray(bw.Bytes()),
|
|
|
|
stackitem.NewByteArray(s.Account.BytesBE()),
|
|
|
|
stackitem.NewBigInteger(big.NewInt(int64(s.Scopes))),
|
|
|
|
stackitem.NewArray(contracts),
|
|
|
|
stackitem.NewArray(groups),
|
|
|
|
stackitem.NewArray(rules),
|
|
|
|
})
|
|
|
|
bw.Reset()
|
|
|
|
}
|
|
|
|
return stackitem.NewArray(res)
|
|
|
|
}
|