Skip to content

Commit

Permalink
update log
Browse files Browse the repository at this point in the history
  • Loading branch information
Jackmeng1985 committed Jun 19, 2023
1 parent db08634 commit b3b8ea2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 52 deletions.
4 changes: 4 additions & 0 deletions common/block/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type Log struct {
BlockNumber *uint256.Int `json:"blockNumber"`
// hash of the transaction
TxHash types.Hash `json:"transactionHash" gencodec:"required"`

// Address of the transaction
TxAddress types.Address `json:"-"`

// index of the transaction in the block
TxIndex uint `json:"transactionIndex" gencodec:"required"`
// hash of the block in which the transaction was included
Expand Down
64 changes: 12 additions & 52 deletions contracts/deposit/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/amazechain/amc/common/crypto"
"github.com/amazechain/amc/common/crypto/bls"
"github.com/amazechain/amc/common/hexutil"
"github.com/amazechain/amc/common/transaction"
"github.com/amazechain/amc/common/types"
"github.com/amazechain/amc/conf"
"github.com/amazechain/amc/log"
Expand Down Expand Up @@ -179,9 +178,9 @@ func (d Deposit) eventLoop() {
if nil != d.consensusConfig.APos && bytes.Compare(l.Address[:], depositContractByes[:]) == 0 {
log.Trace("log event topic[0]= ", "hash", l.Topics[0], "depositEventSignature", depositEventSignature, "withdrawnSignature", withdrawnSignature)
if l.Topics[0] == depositEventSignature {
d.handleDepositEvent(l.TxHash, l.Data)
d.handleDepositEvent(l.TxHash, l.TxAddress, l.Data)
} else if l.Topics[0] == withdrawnSignature {
d.handleWithdrawnEvent(l.TxHash, l.Data)
d.handleWithdrawnEvent(l.TxHash, l.TxAddress, l.Data)
}
}
}
Expand All @@ -190,9 +189,9 @@ func (d Deposit) eventLoop() {
if nil != d.consensusConfig.APos && bytes.Compare(l.Address[:], depositContractByes[:]) == 0 {
log.Trace("log event topic[0]= ", "hash", l.Topics[0], "depositEventSignature", depositEventSignature, "withdrawnSignature", withdrawnSignature)
if l.Topics[0] == depositEventSignature {
d.handleUndoDepositEvent(l.TxHash, l.Data)
d.handleUndoDepositEvent(l.TxHash, l.TxAddress, l.Data)
} else if l.Topics[0] == withdrawnSignature {
d.handleWithdrawnEvent(l.TxHash, l.Data)
d.handleUndoWithdrawnEvent(l.TxHash, l.TxAddress, l.Data)
}
}
}
Expand Down Expand Up @@ -228,7 +227,7 @@ func (d Deposit) verifySignature(sig []byte, pub []byte, depositAmount *uint256.
return nil
}

func (d Deposit) handleDepositEvent(txHash types.Hash, data []byte) {
func (d Deposit) handleDepositEvent(txHash types.Hash, txAddress types.Address, data []byte) {

pb, amount, sig, err := UnpackDepositLogData(data)
if err != nil {
Expand All @@ -248,27 +247,18 @@ func (d Deposit) handleDepositEvent(txHash types.Hash, data []byte) {
return
}

var tx *transaction.Transaction
tx, _, _, _, err = rawdb.ReadTransactionByHash(rwTx, txHash)
if err != nil {
log.Error("cannot read transaction by hash", "err", err, "hash", txHash)
}
if tx == nil {
log.Error("cannot read transaction by hash", "err", err, "hash", txHash)
}

var pub types.PublicKey
_ = pub.SetBytes(pb)
//
if err = rawdb.DoDeposit(rwTx, *tx.From(), pub, amount); err != nil {
if err = rawdb.DoDeposit(rwTx, txAddress, pub, amount); err != nil {
log.Error("cannot modify database", "err", err)
}
if err = rwTx.Commit(); err != nil {
log.Error("cannot commit tx", "err", err)
}
}

func (d Deposit) handleUndoDepositEvent(txHash types.Hash, data []byte) {
func (d Deposit) handleUndoDepositEvent(txHash types.Hash, txAddress types.Address, data []byte) {
pb, amount, sig, err := UnpackDepositLogData(data)
if err != nil {
log.Warn("cannot unpack deposit log data")
Expand All @@ -287,17 +277,8 @@ func (d Deposit) handleUndoDepositEvent(txHash types.Hash, data []byte) {
return
}

var tx *transaction.Transaction
tx, _, _, _, err = rawdb.ReadTransactionByHash(rwTx, txHash)
if err != nil {
log.Error("cannot read transaction by hash", "err", err, "hash", txHash)
}
if tx == nil {
log.Error("cannot read transaction by hash", "err", err, "hash", txHash)
}

//
if err = rawdb.UndoDeposit(rwTx, *tx.From(), amount); err != nil {
if err = rawdb.UndoDeposit(rwTx, txAddress, amount); err != nil {
log.Error("cannot modify database", "err", err)
}
if err = rwTx.Commit(); err != nil {
Expand All @@ -306,7 +287,7 @@ func (d Deposit) handleUndoDepositEvent(txHash types.Hash, data []byte) {

}

func (d Deposit) handleWithdrawnEvent(txHash types.Hash, data []byte) {
func (d Deposit) handleWithdrawnEvent(txHash types.Hash, txAddress types.Address, data []byte) {
// 1
amount, err := UnpackWithdrawnLogData(data)
if err != nil {
Expand All @@ -320,18 +301,8 @@ func (d Deposit) handleWithdrawnEvent(txHash types.Hash, data []byte) {
log.Error("cannot open db", "err", err)
return
}
var tx *transaction.Transaction
tx, _, _, _, err = rawdb.ReadTransactionByHash(rwTx, txHash)
if err != nil {
log.Error("rawdb.ReadTransactionByHash", "err", err, "hash", txHash)
return
}
if tx == nil {
log.Error("cannot find Transaction", "err", err, "hash", txHash)
return
}

err = rawdb.DoWithdrawn(rwTx, *tx.From(), amount)
err = rawdb.DoWithdrawn(rwTx, txAddress, amount)
if err != nil {
log.Error("cannot Withdrawn deposit when handle Withdrawn Event", "err", err)
return
Expand All @@ -341,7 +312,7 @@ func (d Deposit) handleWithdrawnEvent(txHash types.Hash, data []byte) {
}
}

func (d Deposit) handleUndoWithdrawnEvent(txHash types.Hash, data []byte) {
func (d Deposit) handleUndoWithdrawnEvent(txHash types.Hash, txAddress types.Address, data []byte) {
amount, err := UnpackWithdrawnLogData(data)
if err != nil {
log.Warn("cannot unpack deposit log data")
Expand All @@ -350,18 +321,7 @@ func (d Deposit) handleUndoWithdrawnEvent(txHash types.Hash, data []byte) {
rwTx, err := d.db.BeginRw(d.ctx)
defer rwTx.Rollback()

var tx *transaction.Transaction
tx, _, _, _, err = rawdb.ReadTransactionByHash(rwTx, txHash)
if err != nil {
log.Error("rawdb.ReadTransactionByHash", "err", err, "hash", txHash)
return
}
if tx == nil {
log.Error("cannot find Transaction", "err", err, "hash", txHash)
return
}

if err = rawdb.UndoWithdrawn(rwTx, *tx.From(), amount); err != nil {
if err = rawdb.UndoWithdrawn(rwTx, txAddress, amount); err != nil {
log.Error("cannot Undo Withdrawn deposit when handle remove Withdrawn log Event", "err", err)
return
}
Expand Down

0 comments on commit b3b8ea2

Please sign in to comment.