From 1a48f1ce43ec47db735ce558f1772e3197cea4df Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Sat, 27 Jul 2024 12:28:30 +0300 Subject: [PATCH] block: add PrimaryIndex to the stack item of block This is a bad one. $ ./bin/neo-go contract testinvokefunction -r https://rpc10.n3.nspcc.ru:10331 0xda65b600f7124ce6c79950c1772a36403104f2be getBlock 5762000 { "state": "HALT", "gasconsumed": "202812", "script": "AtDrVwARwB8MCGdldEJsb2NrDBS+8gQxQDYqd8FQmcfmTBL3ALZl2kFifVtS", "stack": [ { "type": "Array", "value": [ { "type": "ByteString", "value": "vq5IPTPEDRhz0JA4cQKIa6/o97pnJt/HfVkDRknd1rg=" }, { "type": "Integer", "value": "0" }, { "type": "ByteString", "value": "zFYF3LGaTKdbqVX99shaBUzTq9YjXb0jaPMjk2jdSP4=" }, { "type": "ByteString", "value": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" }, { "type": "Integer", "value": "1722060076994" }, { "type": "Integer", "value": "5293295626238767595" }, { "type": "Integer", "value": "5762000" }, { "type": "ByteString", "value": "LIt05Fpxhl/kXMX3EAGIASyOSQs=" }, { "type": "Integer", "value": "0" } ] } ], "exception": null, "notifications": [] } $ ./bin/neo-go contract testinvokefunction -r http://seed3.neo.org:10332 0xda65b600f7124ce6c79950c1772a36403104f2be getBlock 5762000 { "state": "HALT", "gasconsumed": "202812", "script": "AtDrVwARwB8MCGdldEJsb2NrDBS+8gQxQDYqd8FQmcfmTBL3ALZl2kFifVtS", "stack": [ { "type": "Array", "value": [ { "type": "ByteString", "value": "vq5IPTPEDRhz0JA4cQKIa6/o97pnJt/HfVkDRknd1rg=" }, { "type": "Integer", "value": "0" }, { "type": "ByteString", "value": "zFYF3LGaTKdbqVX99shaBUzTq9YjXb0jaPMjk2jdSP4=" }, { "type": "ByteString", "value": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=" }, { "type": "Integer", "value": "1722060076994" }, { "type": "Integer", "value": "5293295626238767595" }, { "type": "Integer", "value": "5762000" }, { "type": "Integer", "value": "6" }, { "type": "ByteString", "value": "LIt05Fpxhl/kXMX3EAGIASyOSQs=" }, { "type": "Integer", "value": "0" } ] } ], "exception": null, "notifications": [] } 9 fields vs 10, notice the primary index right after the block number. Back when ac527650eb173bb1dbd799d44fea7309fce42ad3 initially added Ledger I've used https://github.com/neo-project/neo/pull/2215 as a reference and it was correct (no primary index). But then https://github.com/neo-project/neo/pull/2296 came into the C# codebase and while it looked like a pure refactoring it actually did add the primary index as well and this wasn't noticed. It wasn't noticed even when 3a4e0caeb8c019fcaaff50090286f5aef634d345 had touched some nearby code. In short, we had a completely wrong implementation of this call for more than three years. But looks like it's not a very popular one. Signed-off-by: Roman Khimov --- pkg/core/block/block.go | 1 + pkg/core/native/native_test/ledger_test.go | 1 + pkg/interop/native/ledger/block.go | 3 +++ 3 files changed, 5 insertions(+) diff --git a/pkg/core/block/block.go b/pkg/core/block/block.go index 32215098b..3e451258f 100644 --- a/pkg/core/block/block.go +++ b/pkg/core/block/block.go @@ -229,6 +229,7 @@ func (b *Block) ToStackItem() stackitem.Item { stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))), stackitem.NewBigInteger(new(big.Int).SetUint64(b.Nonce)), stackitem.NewBigInteger(big.NewInt(int64(b.Index))), + stackitem.NewBigInteger(big.NewInt(int64(b.PrimaryIndex))), stackitem.NewByteArray(b.NextConsensus.BytesBE()), stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))), } diff --git a/pkg/core/native/native_test/ledger_test.go b/pkg/core/native/native_test/ledger_test.go index a7d878aef..308120b21 100644 --- a/pkg/core/native/native_test/ledger_test.go +++ b/pkg/core/native/native_test/ledger_test.go @@ -159,6 +159,7 @@ func TestLedger_GetBlock(t *testing.T) { stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))), stackitem.NewBigInteger(big.NewInt(int64(b.Nonce))), stackitem.NewBigInteger(big.NewInt(int64(b.Index))), + stackitem.NewBigInteger(big.NewInt(int64(b.PrimaryIndex))), stackitem.NewByteArray(b.NextConsensus.BytesBE()), stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))), } diff --git a/pkg/interop/native/ledger/block.go b/pkg/interop/native/ledger/block.go index 0164803cb..58c88521a 100644 --- a/pkg/interop/native/ledger/block.go +++ b/pkg/interop/native/ledger/block.go @@ -23,6 +23,8 @@ type Block struct { Nonce int // Index represents the height of the block. Index int + // PrimaryIndex represents the index of the primary node that created this block. + PrimaryIndex int // NextConsensus represents the contract address of the next miner (160 bit BE // value in a 20 byte slice). NextConsensus interop.Hash160 @@ -43,6 +45,7 @@ type BlockSR struct { Timestamp int Nonce int Index int + PrimaryIndex int NextConsensus interop.Hash160 TransactionsLength int // PrevStateRoot is a hash of the previous block's state root.