-
Notifications
You must be signed in to change notification settings - Fork 203
Description
Problem
In storage/indexes/contracts.go, the makeContractDeploymentKey function encodes height in
descending order (using one's complement ^height) so that the most recent deployment appears
first during ascending key iteration. However, txIndex and eventIndex are encoded in
ascending order.
This means if the same contract is updated multiple times within a single block, lexicographic
ordering will return the earliest transaction/event in that block instead of the latest.
In practice, it is very unlikely for a contract to be updated multiple times within a single block,
so this is not an urgent issue. However, we should fix it for correctness before rolling out
publicly.
Fix
Apply one's complement to txIndex and eventIndex in both the encoding
(makeContractDeploymentKey) and decoding (decodeDeploymentCursor) functions:
Encoding (lines ~366-369):
binary.BigEndian.PutUint32(key[offset:], ^txIndex)
binary.BigEndian.PutUint32(key[offset:], ^eventIndex)Decoding (lines ~443-446):
txIndex := ^binary.BigEndian.Uint32(key[offset:])
eventIndex := ^binary.BigEndian.Uint32(key[offset:])Reindexing
Since this changes the key encoding, nodes that have already indexed contract deployments will
have data stored with the old encoding. A tool will be needed to delete the existing contracts
index data so the index can be rebuilt from scratch on affected nodes.