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 ac527650eb 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 3a4e0caeb8 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 <roman@nspcc.ru>
This commit is contained in:
Roman Khimov 2024-07-27 12:28:30 +03:00
parent c6ed92a169
commit 1a48f1ce43
3 changed files with 5 additions and 0 deletions

View file

@ -229,6 +229,7 @@ func (b *Block) ToStackItem() stackitem.Item {
stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))), stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))),
stackitem.NewBigInteger(new(big.Int).SetUint64(b.Nonce)), stackitem.NewBigInteger(new(big.Int).SetUint64(b.Nonce)),
stackitem.NewBigInteger(big.NewInt(int64(b.Index))), stackitem.NewBigInteger(big.NewInt(int64(b.Index))),
stackitem.NewBigInteger(big.NewInt(int64(b.PrimaryIndex))),
stackitem.NewByteArray(b.NextConsensus.BytesBE()), stackitem.NewByteArray(b.NextConsensus.BytesBE()),
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))), stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
} }

View file

@ -159,6 +159,7 @@ func TestLedger_GetBlock(t *testing.T) {
stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))), stackitem.NewBigInteger(big.NewInt(int64(b.Timestamp))),
stackitem.NewBigInteger(big.NewInt(int64(b.Nonce))), stackitem.NewBigInteger(big.NewInt(int64(b.Nonce))),
stackitem.NewBigInteger(big.NewInt(int64(b.Index))), stackitem.NewBigInteger(big.NewInt(int64(b.Index))),
stackitem.NewBigInteger(big.NewInt(int64(b.PrimaryIndex))),
stackitem.NewByteArray(b.NextConsensus.BytesBE()), stackitem.NewByteArray(b.NextConsensus.BytesBE()),
stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))), stackitem.NewBigInteger(big.NewInt(int64(len(b.Transactions)))),
} }

View file

@ -23,6 +23,8 @@ type Block struct {
Nonce int Nonce int
// Index represents the height of the block. // Index represents the height of the block.
Index int 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 // NextConsensus represents the contract address of the next miner (160 bit BE
// value in a 20 byte slice). // value in a 20 byte slice).
NextConsensus interop.Hash160 NextConsensus interop.Hash160
@ -43,6 +45,7 @@ type BlockSR struct {
Timestamp int Timestamp int
Nonce int Nonce int
Index int Index int
PrimaryIndex int
NextConsensus interop.Hash160 NextConsensus interop.Hash160
TransactionsLength int TransactionsLength int
// PrevStateRoot is a hash of the previous block's state root. // PrevStateRoot is a hash of the previous block's state root.