Skip to content

Commit e5b1280

Browse files
authored
run-evm-sync
Run evm sync
2 parents f653b79 + 1b5d6cf commit e5b1280

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

model/model_db.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,13 @@ type Trace struct {
6060

6161
type ContractLifecycle struct {
6262
Model
63-
TxId uint64 `gorm:"not null" json:"txId"`
64-
TxSenderId uint64 `gorm:"not null" json:"txSenderId"`
65-
ContractId uint64 `gorm:"not null" json:"contractId"`
66-
Event string `gorm:"not null" json:"event"`
67-
RefundAddrId uint64 `gorm:"not null" json:"refundAddrId"`
68-
Value decimal.Decimal `gorm:"type:decimal(36,18);not null" json:"value"`
63+
TxId uint64 `gorm:"not null" json:"txId"`
64+
TransactionPosition uint `json:"transactionPosition"`
65+
TxSenderId uint64 `gorm:"not null" json:"txSenderId"`
66+
ContractId uint64 `gorm:"not null" json:"contractId"`
67+
Event string `gorm:"not null" json:"event"`
68+
RefundAddrId uint64 `gorm:"not null" json:"refundAddrId"`
69+
Value decimal.Decimal `gorm:"type:decimal(36,18);not null" json:"value"`
6970
}
7071

7172
const (

sync/evm/evm_sync.go

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ type TraceProcessor struct {
2222
}
2323

2424
type TraceOperation struct {
25-
dbBlock *model.Block
26-
minerAddr *model.Address
27-
traceArr []*model.Trace
28-
txArr []*model.Tx
29-
Err error
25+
dbBlock *model.Block
26+
minerAddr *model.Address
27+
traceArr []*model.Trace
28+
txArr []*model.Tx
29+
contractLifecycleArr []*model.ContractLifecycle
30+
Err error
3031
}
3132

3233
func newErrOperation(err error) *TraceOperation {
@@ -47,7 +48,9 @@ func (o *TraceOperation) Exec(tx *gorm.DB) error {
4748
o.dbBlock.MinerID = addrPre.ID
4849

4950
//save block
50-
tx.Create(&o.dbBlock)
51+
if err := tx.Create(&o.dbBlock).Error; err != nil {
52+
return errors.Wrap(err, "create block failed")
53+
}
5154

5255
// update block id for tx record
5356
for _, txPO := range o.txArr {
@@ -71,6 +74,15 @@ func (o *TraceOperation) Exec(tx *gorm.DB) error {
7174
}
7275
}
7376

77+
if len(o.contractLifecycleArr) > 0 {
78+
for _, lifecycle := range o.contractLifecycleArr {
79+
lifecycle.TxId = o.txArr[lifecycle.TransactionPosition].ID
80+
}
81+
if err := tx.Create(&o.contractLifecycleArr).Error; err != nil {
82+
return errors.Wrap(err, "create contract lifecycle")
83+
}
84+
}
85+
7486
return nil
7587
}
7688

@@ -150,10 +162,11 @@ func (t *TraceProcessor) Process(data evmUtil.BlockData) dbUtil.Operation {
150162
Model: model.Model{
151163
CreatedAt: dbBlock.CreatedAt,
152164
},
153-
TxSenderId: txArr[*trace.TransactionPosition].FromId,
154-
ContractId: dbTrace.ToId,
155-
Value: decimal.NewFromBigInt(create.Value, -18),
156-
Event: model.ContractLifecycleCreate,
165+
TransactionPosition: *trace.TransactionPosition,
166+
TxSenderId: txArr[*trace.TransactionPosition].FromId,
167+
ContractId: dbTrace.ToId,
168+
Value: decimal.NewFromBigInt(create.Value, -18),
169+
Event: model.ContractLifecycleCreate,
157170
})
158171
case types.TRACE_SUICIDE:
159172
suicide, _ := trace.Action.(types.Suicide)
@@ -185,10 +198,11 @@ func (t *TraceProcessor) Process(data evmUtil.BlockData) dbUtil.Operation {
185198
}
186199

187200
return &TraceOperation{
188-
dbBlock: &dbBlock,
189-
minerAddr: &dbAddr,
190-
traceArr: traceArr,
191-
txArr: txArr,
201+
dbBlock: &dbBlock,
202+
minerAddr: &dbAddr,
203+
traceArr: traceArr,
204+
txArr: txArr,
205+
contractLifecycleArr: contractLifecycleArr,
192206
}
193207
}
194208

@@ -199,7 +213,11 @@ func buildTxFromReceipt(db *gorm.DB, receipts []*types.Receipt, blockTime time.T
199213
if err != nil {
200214
return nil, err
201215
}
202-
toAddr, err := model.MakeAddrId(db, receipt.To.Hex(), blockTime)
216+
to := receipt.To
217+
if to == nil {
218+
to = receipt.ContractAddress
219+
}
220+
toAddr, err := model.MakeAddrId(db, to.Hex(), blockTime)
203221
if err != nil {
204222
return nil, err
205223
}

0 commit comments

Comments
 (0)