Skip to content

Commit aa4339f

Browse files
authored
Merge pull request #51 from vulcanize/v1.9.25-statediff-0.0.14
add poststate and status to receipt ipld indexes
2 parents 283fc6d + 081437c commit aa4339f

File tree

5 files changed

+66
-45
lines changed

5 files changed

+66
-45
lines changed

statediff/indexer/indexer.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -299,27 +299,18 @@ func (sdi *StateDiffIndexer) processReceiptsAndTxs(tx *sqlx.Tx, args processArgs
299299
// this is the contract address if this receipt is for a contract creation tx
300300
contract := shared.HandleZeroAddr(receipt.ContractAddress)
301301
var contractHash string
302-
isDeployment := contract != ""
303-
if isDeployment {
302+
if contract != "" {
304303
contractHash = crypto.Keccak256Hash(common.HexToAddress(contract).Bytes()).String()
305-
// if tx is a contract deployment, publish the data (code)
306-
// codec doesn't matter in this case sine we are not interested in the cid and the db key is multihash-derived
307-
// TODO: THE DATA IS NOT DIRECTLY THE CONTRACT CODE; THERE IS A MISSING PROCESSING STEP HERE
308-
// the contractHash => contract code is not currently correct
309-
if _, err := shared.PublishRaw(tx, ipld.MEthStorageTrie, multihash.KECCAK_256, trx.Data()); err != nil {
310-
return err
311-
}
312304
}
313305
// index tx first so that the receipt can reference it by FK
314306
txModel := models.TxModel{
315-
Dst: shared.HandleZeroAddrPointer(trx.To()),
316-
Src: shared.HandleZeroAddr(from),
317-
TxHash: trx.Hash().String(),
318-
Index: int64(i),
319-
Data: trx.Data(),
320-
Deployment: isDeployment,
321-
CID: txNode.Cid().String(),
322-
MhKey: shared.MultihashKeyFromCID(txNode.Cid()),
307+
Dst: shared.HandleZeroAddrPointer(trx.To()),
308+
Src: shared.HandleZeroAddr(from),
309+
TxHash: trx.Hash().String(),
310+
Index: int64(i),
311+
Data: trx.Data(),
312+
CID: txNode.Cid().String(),
313+
MhKey: shared.MultihashKeyFromCID(txNode.Cid()),
323314
}
324315
txID, err := sdi.dbWriter.upsertTransactionCID(tx, txModel, args.headerID)
325316
if err != nil {
@@ -337,6 +328,11 @@ func (sdi *StateDiffIndexer) processReceiptsAndTxs(tx *sqlx.Tx, args processArgs
337328
CID: rctNode.Cid().String(),
338329
MhKey: shared.MultihashKeyFromCID(rctNode.Cid()),
339330
}
331+
if len(receipt.PostState) == 0 {
332+
rctModel.PostStatus = receipt.Status
333+
} else {
334+
rctModel.PostState = common.Bytes2Hex(receipt.PostState)
335+
}
340336
if err := sdi.dbWriter.upsertReceiptCID(tx, rctModel, txID); err != nil {
341337
return err
342338
}

statediff/indexer/indexer_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,10 +183,31 @@ func TestPublishAndIndexer(t *testing.T) {
183183
switch c {
184184
case mocks.Rct1CID.String():
185185
shared.ExpectEqual(t, data, mocks.MockReceipts.GetRlp(0))
186+
var postStatus uint64
187+
pgStr = `SELECT post_status FROM eth.receipt_cids WHERE cid = $1`
188+
err = db.Get(&postStatus, pgStr, c)
189+
if err != nil {
190+
t.Fatal(err)
191+
}
192+
shared.ExpectEqual(t, postStatus, mocks.ExpectedPostStatus)
186193
case mocks.Rct2CID.String():
187194
shared.ExpectEqual(t, data, mocks.MockReceipts.GetRlp(1))
195+
var postState string
196+
pgStr = `SELECT post_state FROM eth.receipt_cids WHERE cid = $1`
197+
err = db.Get(&postState, pgStr, c)
198+
if err != nil {
199+
t.Fatal(err)
200+
}
201+
shared.ExpectEqual(t, postState, mocks.ExpectedPostState1)
188202
case mocks.Rct3CID.String():
189203
shared.ExpectEqual(t, data, mocks.MockReceipts.GetRlp(2))
204+
var postState string
205+
pgStr = `SELECT post_state FROM eth.receipt_cids WHERE cid = $1`
206+
err = db.Get(&postState, pgStr, c)
207+
if err != nil {
208+
t.Fatal(err)
209+
}
210+
shared.ExpectEqual(t, postState, mocks.ExpectedPostState2)
190211
}
191212
}
192213
})

statediff/indexer/mocks/test_data.go

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,23 @@ var (
5151
Difficulty: big.NewInt(5000000),
5252
Extra: []byte{},
5353
}
54-
MockTransactions, MockReceipts, SenderAddr = createTransactionsAndReceipts()
55-
ReceiptsRlp, _ = rlp.EncodeToBytes(MockReceipts)
56-
MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts, new(trie.Trie))
57-
MockHeaderRlp, _ = rlp.EncodeToBytes(MockBlock.Header())
58-
Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592")
59-
AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593")
60-
ContractAddress = crypto.CreateAddress(SenderAddr, MockTransactions[2].Nonce())
61-
ContractHash = crypto.Keccak256Hash(ContractAddress.Bytes()).String()
62-
MockContractByteCode = []byte{0, 1, 2, 3, 4, 5}
63-
mockTopic11 = common.HexToHash("0x04")
64-
mockTopic12 = common.HexToHash("0x06")
65-
mockTopic21 = common.HexToHash("0x05")
66-
mockTopic22 = common.HexToHash("0x07")
67-
MockLog1 = &types.Log{
54+
MockTransactions, MockReceipts, SenderAddr = createTransactionsAndReceipts()
55+
ReceiptsRlp, _ = rlp.EncodeToBytes(MockReceipts)
56+
MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts, new(trie.Trie))
57+
MockHeaderRlp, _ = rlp.EncodeToBytes(MockBlock.Header())
58+
Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592")
59+
AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593")
60+
ContractAddress = crypto.CreateAddress(SenderAddr, MockTransactions[2].Nonce())
61+
ContractHash = crypto.Keccak256Hash(ContractAddress.Bytes()).String()
62+
MockContractByteCode = []byte{0, 1, 2, 3, 4, 5}
63+
mockTopic11 = common.HexToHash("0x04")
64+
mockTopic12 = common.HexToHash("0x06")
65+
mockTopic21 = common.HexToHash("0x05")
66+
mockTopic22 = common.HexToHash("0x07")
67+
ExpectedPostStatus uint64 = 1
68+
ExpectedPostState1 = common.Bytes2Hex(common.HexToHash("0x1").Bytes())
69+
ExpectedPostState2 = common.Bytes2Hex(common.HexToHash("0x2").Bytes())
70+
MockLog1 = &types.Log{
6871
Address: Address,
6972
Topics: []common.Hash{mockTopic11, mockTopic12},
7073
Data: []byte{},
@@ -181,7 +184,7 @@ func createTransactionsAndReceipts() (types.Transactions, types.Receipts, common
181184
log.Crit(err.Error())
182185
}
183186
// make receipts
184-
mockReceipt1 := types.NewReceipt(common.HexToHash("0x0").Bytes(), false, 50)
187+
mockReceipt1 := types.NewReceipt(nil, false, 50)
185188
mockReceipt1.Logs = []*types.Log{MockLog1}
186189
mockReceipt1.TxHash = signedTrx1.Hash()
187190
mockReceipt2 := types.NewReceipt(common.HexToHash("0x1").Bytes(), false, 100)

statediff/indexer/models/models.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,15 @@ type UncleModel struct {
5151

5252
// TxModel is the db model for eth.transaction_cids
5353
type TxModel struct {
54-
ID int64 `db:"id"`
55-
HeaderID int64 `db:"header_id"`
56-
Index int64 `db:"index"`
57-
TxHash string `db:"tx_hash"`
58-
CID string `db:"cid"`
59-
MhKey string `db:"mh_key"`
60-
Dst string `db:"dst"`
61-
Src string `db:"src"`
62-
Data []byte `db:"tx_data"`
63-
Deployment bool `db:"deployment"`
54+
ID int64 `db:"id"`
55+
HeaderID int64 `db:"header_id"`
56+
Index int64 `db:"index"`
57+
TxHash string `db:"tx_hash"`
58+
CID string `db:"cid"`
59+
MhKey string `db:"mh_key"`
60+
Dst string `db:"dst"`
61+
Src string `db:"src"`
62+
Data []byte `db:"tx_data"`
6463
}
6564

6665
// ReceiptModel is the db model for eth.receipt_cids
@@ -69,6 +68,8 @@ type ReceiptModel struct {
6968
TxID int64 `db:"tx_id"`
7069
CID string `db:"cid"`
7170
MhKey string `db:"mh_key"`
71+
PostStatus uint64 `db:"post_status"`
72+
PostState string `db:"post_state"`
7273
Contract string `db:"contract"`
7374
ContractHash string `db:"contract_hash"`
7475
LogContracts pq.StringArray `db:"log_contracts"`

statediff/indexer/writer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ func (in *PostgresCIDWriter) upsertTransactionCID(tx *sqlx.Tx, transaction model
9696
}
9797

9898
func (in *PostgresCIDWriter) upsertReceiptCID(tx *sqlx.Tx, rct models.ReceiptModel, txID int64) error {
99-
_, err := tx.Exec(`INSERT INTO eth.receipt_cids (tx_id, cid, contract, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts, mh_key) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
100-
ON CONFLICT (tx_id) DO UPDATE SET (cid, contract, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts, mh_key) = ($2, $3, $4, $5, $6, $7, $8, $9, $10)`,
101-
txID, rct.CID, rct.Contract, rct.ContractHash, rct.Topic0s, rct.Topic1s, rct.Topic2s, rct.Topic3s, rct.LogContracts, rct.MhKey)
99+
_, err := tx.Exec(`INSERT INTO eth.receipt_cids (tx_id, cid, contract, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts, mh_key, post_state, post_status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
100+
ON CONFLICT (tx_id) DO UPDATE SET (cid, contract, contract_hash, topic0s, topic1s, topic2s, topic3s, log_contracts, mh_key, post_state, post_status) = ($2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
101+
txID, rct.CID, rct.Contract, rct.ContractHash, rct.Topic0s, rct.Topic1s, rct.Topic2s, rct.Topic3s, rct.LogContracts, rct.MhKey, rct.PostState, rct.PostStatus)
102102
if err == nil {
103103
indexerMetrics.receipts.Inc(1)
104104
}

0 commit comments

Comments
 (0)