core: save NEP5 transfer notify index

TransferNotifyIndex is the index of transfer event in the list
of all transfers in a transaction.
This commit is contained in:
Evgenii Stratonikov 2020-08-03 10:43:53 +03:00
parent a702423c4a
commit c6d33c5841
6 changed files with 35 additions and 17 deletions

View file

@ -799,6 +799,7 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
if err != nil {
return errors.Wrap(err, "failed to persist invocation results")
}
var index uint32
for _, note := range systemInterop.notifications {
arr, ok := note.Item.Value().([]vm.StackItem)
if !ok || len(arr) != 4 {
@ -824,7 +825,8 @@ func (bc *Blockchain) storeBlock(block *block.Block) error {
}
amount = emit.BytesToInt(bs)
}
bc.processNEP5Transfer(cache, tx, block, note.ScriptHash, from, to, amount.Int64())
bc.processNEP5Transfer(cache, tx, block, note.ScriptHash, from, to, amount.Int64(), index)
index++
}
} else {
bc.log.Warn("contract invocation failed",
@ -911,7 +913,7 @@ func parseUint160(addr []byte) util.Uint160 {
return util.Uint160{}
}
func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, tx *transaction.Transaction, b *block.Block, sc util.Uint160, from, to []byte, amount int64) {
func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, tx *transaction.Transaction, b *block.Block, sc util.Uint160, from, to []byte, amount int64, index uint32) {
toAddr := parseUint160(to)
fromAddr := parseUint160(from)
transfer := &state.NEP5Transfer{
@ -921,6 +923,7 @@ func (bc *Blockchain) processNEP5Transfer(cache *dao.Cached, tx *transaction.Tra
Block: b.Index,
Timestamp: b.Timestamp,
Tx: tx.Hash(),
Index: index,
}
if !fromAddr.Equals(util.Uint160{}) {
balances, err := cache.GetNEP5Balances(fromAddr)

View file

@ -409,7 +409,7 @@ func TestCreateBasicChain(t *testing.T) {
sh = hash.Hash160(avm)
t.Logf("contractHash (new): %s", sh.StringLE())
transferTx = newNEP5Transfer(sh, priv1.GetScriptHash(), priv0.GetScriptHash(), 3)
transferTx = newNEP5Transfer(sh, priv1.GetScriptHash(), priv0.GetScriptHash(), 2, 1)
b = bc.newBlock(newMinerTX(), transferTx)
require.NoError(t, bc.AddBlock(b))
@ -446,10 +446,12 @@ func TestCreateBasicChain(t *testing.T) {
}
}
func newNEP5Transfer(sc, from, to util.Uint160, amount int64) *transaction.Transaction {
func newNEP5Transfer(sc, from, to util.Uint160, amounts ...int64) *transaction.Transaction {
w := io.NewBufBinWriter()
emit.AppCallWithOperationAndArgs(w.BinWriter, sc, "transfer", from, to, amount)
for i := range amounts {
emit.AppCallWithOperationAndArgs(w.BinWriter, sc, "transfer", from, to, amounts[i])
emit.Opcode(w.BinWriter, opcode.THROWIFNOT)
}
script := w.Bytes()
return transaction.NewInvocationTX(script, 0)

View file

@ -20,7 +20,7 @@ type NEP5TransferLog struct {
}
// NEP5TransferSize is a size of a marshaled NEP5Transfer struct in bytes.
const NEP5TransferSize = util.Uint160Size*3 + 8 + 4 + 4 + util.Uint256Size
const NEP5TransferSize = util.Uint160Size*3 + 8 + 4 + 4 + util.Uint256Size + 4
// NEP5Transfer represents a single NEP5 Transfer event.
type NEP5Transfer struct {
@ -39,6 +39,8 @@ type NEP5Transfer struct {
Timestamp uint32
// Tx is a hash the transaction.
Tx util.Uint256
// Index is the index of this transfer in the corresponding tx.
Index uint32
}
// NEP5Balances is a map of the NEP5 contract hashes
@ -152,6 +154,7 @@ func (t *NEP5Transfer) EncodeBinary(w *io.BinWriter) {
w.WriteU32LE(t.Block)
w.WriteU32LE(t.Timestamp)
w.WriteU64LE(uint64(t.Amount))
w.WriteU32LE(t.Index)
}
// DecodeBinary implements io.Serializable interface.
@ -163,4 +166,5 @@ func (t *NEP5Transfer) DecodeBinary(r *io.BinReader) {
t.Block = r.ReadU32LE()
t.Timestamp = r.ReadU32LE()
t.Amount = int64(r.ReadU64LE())
t.Index = r.ReadU32LE()
}

View file

@ -613,6 +613,8 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
Asset: tr.Asset,
Index: tr.Block,
TxHash: tr.Tx,
NotifyIndex: tr.Index,
}
d, err := s.getDecimals(tr.Asset, cache)
if err != nil {

View file

@ -447,25 +447,25 @@ var rpcTestCases = map[string][]rpcTestCase{
"getblockheader": {
{
name: "positive, no verbose",
params: `["eff4bb259b1d8bce877f132cbbdee7697c8fcf10154a16faaec4e71410c270c5"]`,
params: `["ac8239ca86a56ea02c66bc22c17bad194dd35d4e6391359435ab0a04af115fdf"]`,
result: func(e *executor) interface{} {
expected := "00000000999086db552ba8f84734bddca55b25a8d3d8c5f866f941209169c38d35376e995f2f29c4685140d85073fec705089706553eae4de3c95d9d8d425af36e597ee633ff165f010000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd040140867ab10b2312fba2d9324f329936ccc938ba9f1ccfa5608b9ad4683ccafab9f1934b4e5a2ac784b97de02e12e97c20f159080cbd01493d0c77ddb184e18a0dbe40464ad05fa90bedfbae29b517532dc913dd1a0042801218b111d1cfb205ca2631b9246c28610055773209b0f82409b7218a0fbd1aa281b446edac31b87bd23b39404b87fef7696d42a32da3dfdcaa25cbb2db434a461bcadb5c91b3f078ab2cba960b5dad2d25b8c8423cf8574a4598fd7608f321d075a30e6327425e96a5cb306140d3527a08477ba441f2ba46256ff0d691c4bef0af1e8b80d65be9b0a5f350b15d130651725ab11bd1581a4ddfbc2486784a5057f727ea2d7d079d50153be17ce78b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae00"
expected := "00000000999086db552ba8f84734bddca55b25a8d3d8c5f866f941209169c38d35376e995f2f29c4685140d85073fec705089706553eae4de3c95d9d8d425af36e597ee6bdc2275f010000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd040140e2ce9b2936c858ec8d8e2ae0dc3e1d924700efb44eecfccc6a7de134ff9b7505edf3e536fe6810f5ce585f6a2976b6bcf9b52addd2234ab73a063a7f52c07988401f91f717a3a386441d05a5c64a4e834299fd56d4f86c8e0eafc4d91e7e013d14a4eaff4d113aef76dedd93c326e9fe9f81e2ee0995ac20f87d56ef7c23bbea39400cf601281f67a69119fad0ef04e5c600b602ba36a490b1e32f006a9135bc5cb4359b855202680a7a07c03d16404738bbf7253c22013f67b2cc2a2a47640b3a63404cd3181b886fe64cf01caad4b4aa0c70587bd0fbcbc2d9cdd15bbc11878159a0da7341be6d06627e614d9e48529c7c96d4b9a4a56aca4fa118f1b49c2789e82b8b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae00"
return &expected
},
},
{
name: "positive, verbose 0",
params: `["eff4bb259b1d8bce877f132cbbdee7697c8fcf10154a16faaec4e71410c270c5", 0]`,
params: `["ac8239ca86a56ea02c66bc22c17bad194dd35d4e6391359435ab0a04af115fdf", 0]`,
result: func(e *executor) interface{} {
expected := "00000000999086db552ba8f84734bddca55b25a8d3d8c5f866f941209169c38d35376e995f2f29c4685140d85073fec705089706553eae4de3c95d9d8d425af36e597ee633ff165f010000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd040140867ab10b2312fba2d9324f329936ccc938ba9f1ccfa5608b9ad4683ccafab9f1934b4e5a2ac784b97de02e12e97c20f159080cbd01493d0c77ddb184e18a0dbe40464ad05fa90bedfbae29b517532dc913dd1a0042801218b111d1cfb205ca2631b9246c28610055773209b0f82409b7218a0fbd1aa281b446edac31b87bd23b39404b87fef7696d42a32da3dfdcaa25cbb2db434a461bcadb5c91b3f078ab2cba960b5dad2d25b8c8423cf8574a4598fd7608f321d075a30e6327425e96a5cb306140d3527a08477ba441f2ba46256ff0d691c4bef0af1e8b80d65be9b0a5f350b15d130651725ab11bd1581a4ddfbc2486784a5057f727ea2d7d079d50153be17ce78b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae00"
expected := "00000000999086db552ba8f84734bddca55b25a8d3d8c5f866f941209169c38d35376e995f2f29c4685140d85073fec705089706553eae4de3c95d9d8d425af36e597ee6bdc2275f010000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd040140e2ce9b2936c858ec8d8e2ae0dc3e1d924700efb44eecfccc6a7de134ff9b7505edf3e536fe6810f5ce585f6a2976b6bcf9b52addd2234ab73a063a7f52c07988401f91f717a3a386441d05a5c64a4e834299fd56d4f86c8e0eafc4d91e7e013d14a4eaff4d113aef76dedd93c326e9fe9f81e2ee0995ac20f87d56ef7c23bbea39400cf601281f67a69119fad0ef04e5c600b602ba36a490b1e32f006a9135bc5cb4359b855202680a7a07c03d16404738bbf7253c22013f67b2cc2a2a47640b3a63404cd3181b886fe64cf01caad4b4aa0c70587bd0fbcbc2d9cdd15bbc11878159a0da7341be6d06627e614d9e48529c7c96d4b9a4a56aca4fa118f1b49c2789e82b8b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae00"
return &expected
},
},
{
name: "positive, verbose !=0",
params: `["eff4bb259b1d8bce877f132cbbdee7697c8fcf10154a16faaec4e71410c270c5", 2]`,
params: `["ac8239ca86a56ea02c66bc22c17bad194dd35d4e6391359435ab0a04af115fdf", 2]`,
result: func(e *executor) interface{} {
hash, err := util.Uint256DecodeStringLE("eff4bb259b1d8bce877f132cbbdee7697c8fcf10154a16faaec4e71410c270c5")
hash, err := util.Uint256DecodeStringLE("ac8239ca86a56ea02c66bc22c17bad194dd35d4e6391359435ab0a04af115fdf")
if err != nil {
panic("can not decode hash parameter")
}
@ -497,7 +497,7 @@ var rpcTestCases = map[string][]rpcTestCase{
},
{
name: "invalid verbose type",
params: `["eff4bb259b1d8bce877f132cbbdee7697c8fcf10154a16faaec4e71410c270c5", true]`,
params: `["ac8239ca86a56ea02c66bc22c17bad194dd35d4e6391359435ab0a04af115fdf", true]`,
fail: true,
},
{
@ -889,7 +889,7 @@ var rpcTestCases = map[string][]rpcTestCase{
{
name: "positive",
// If you are planning to modify test chain from `testblocks.acc`, please, update param value
params: `["00000000c09fa5d15c48fc4d444a5cd103cbcfc17d7f336a9470342050a0d543c50e4603edb908054ac1409be5f77d5369c6e03490b2f6676d68d0b3370f8159e0fdadf90500175fd30000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd04014020f6f25257442110f3768ccff694b6f35c46a92859445f80cc57938a69b91bb7c23d1b87eab2e1a2c1fff14d264f2017baefc0d5c0ae586b203782fdd0f5a43d40b885005b735818d30294c5a477c1b736eac396be90a0f741511911efdd76fa47671bd02fa4161a443f84e02639d5ffbc20a341836cbc224a5471afbd89b4e82b40f089b04230dd5f0e872b2b1bfa12120481f0a79b110221a325d00101d6558c38888dcc0cc78f911d6037dfe7a41d1befeb9e055adf80597a0c4fca8fe8f0bb2440d6eb1075244c49cf785e093a60db3532acc7387e723d70562fd9ac74477b149047c6a09909c146906d2ac6c430fbfaead3c151ffa8a31b20db18e7ee43b1e22f8b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae0100000000000000000000"]`,
params: `["00000000cd1bab8dadfbe297367aa85d77b9b31ea3e4c4a09285eca299fd124dec280dc9edb908054ac1409be5f77d5369c6e03490b2f6676d68d0b3370f8159e0fdadf98fc3275fd30000005704000000000000be48d3a3f5d10013ab9ffee489706078714f1ea201fd0401401dfd7f28a8c86acc58691d48db403ec29dcc342a0caf0f0ac4ee0329a94772ef05065c8c60921d8931d249b96b19a6059836fd489c8f55bce5e0ff94bc333e19400200f5f0b1c0eafdf41d30277004d987413cac71cc8fc5c73ba23274a18265d63e58fbc94f0118b9fa40411a0f75e478bccf52a8ab885881c443a69d56797f82409ee263f7699cb7b2c73955345a3354410322cad43a197de7138ef37e0fb1061e43cd907d1da725ca5a658babcfb23ed87064b639979b97b95686b0cc58d6d729403fa7fbe626bd60f452fd812ea2cefe81ee4bb374103a28dfe81d385c2d2c9925af3fd32d2868d7321db5b025e7ddf6ab27ac6d37fdfbbe3bae858fd4129128498b532102103a7f7dd016558597f7960d27c516a4394fd968b9e65155eb4b013e4040406e2102a7bc55fe8684e0119768d104ba30795bdcc86619e864add26156723ed185cd622102b3622bf4017bdfe317c58aed5f4c753f206b7db896046fa7d774bbc4bf7f8dc22103d90c07df63e690ce77912e10ab51acc944b66860237b608c4f8f8309e71ee69954ae0100000000000000000000"]`,
result: func(e *executor) interface{} {
v := true
return &v
@ -1169,17 +1169,24 @@ func checkNep5Transfers(t *testing.T, e *executor, acc interface{}) {
assetHashOld, err := util.Uint160DecodeStringLE(testContractHashOld)
require.NoError(t, err)
require.Equal(t, 2, len(res.Received))
require.Equal(t, 3, len(res.Received))
require.Equal(t, "10", res.Received[0].Amount)
require.Equal(t, assetHashOld, res.Received[0].Asset)
require.Equal(t, address.Uint160ToString(assetHashOld), res.Received[0].Address)
require.Equal(t, "0.03", res.Received[1].Amount)
require.Equal(t, "0.02", res.Received[1].Amount)
require.Equal(t, assetHash, res.Received[1].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[1].Address)
require.Equal(t, uint32(0), res.Received[1].NotifyIndex)
require.Equal(t, "0.01", res.Received[2].Amount)
require.Equal(t, assetHash, res.Received[2].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[2].Address)
require.Equal(t, uint32(1), res.Received[2].NotifyIndex)
require.Equal(t, 1, len(res.Sent))
require.Equal(t, "1.23", res.Sent[0].Amount)
require.Equal(t, assetHashOld, res.Sent[0].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Sent[0].Address)
require.Equal(t, uint32(0), res.Sent[0].NotifyIndex)
}

Binary file not shown.