Save Deposit to DB Even If Fails to Deserialize (#2440)

* include bad deposits in the db

* fmt

* Update beacon-chain/powchain/log_processing.go

Co-Authored-By: rauljordan <raul@prysmaticlabs.com>

* lint

* comment
This commit is contained in:
Raul Jordan
2019-04-29 14:50:18 -05:00
committed by GitHub
parent 63998b92aa
commit aa80c308ce
2 changed files with 25 additions and 12 deletions

View File

@@ -76,10 +76,11 @@ func (w *Web3Service) ProcessDepositLog(depositLog gethTypes.Log) {
// We then decode the deposit input in order to create a deposit object
// we can store in our persistent DB.
validData := true
depositInput, err := helpers.DecodeDepositInput(depositData)
if err != nil {
log.Errorf("Could not decode deposit input %v", err)
return
validData = false
}
deposit := &pb.Deposit{
@@ -87,14 +88,11 @@ func (w *Web3Service) ProcessDepositLog(depositLog gethTypes.Log) {
MerkleTreeIndex: index,
}
validData := true
// Make sure duplicates are rejected pre-chainstart.
if !w.chainStarted {
if !w.chainStarted && validData {
var pubkey = fmt.Sprintf("#%x", depositInput.Pubkey)
if w.beaconDB.PubkeyInChainstart(w.ctx, pubkey) {
log.Warnf("Pubkey %#x has already been submitted for chainstart", pubkey)
validData = false
} else {
w.beaconDB.MarkPubkeyForChainstart(w.ctx, pubkey)
}
@@ -103,17 +101,21 @@ func (w *Web3Service) ProcessDepositLog(depositLog gethTypes.Log) {
// We always store all historical deposits in the DB.
w.beaconDB.InsertDeposit(w.ctx, deposit, big.NewInt(int64(depositLog.BlockNumber)))
if !w.chainStarted {
w.chainStartDeposits = append(w.chainStartDeposits, depositData)
} else {
w.beaconDB.InsertPendingDeposit(w.ctx, deposit, big.NewInt(int64(depositLog.BlockNumber)))
}
if validData {
if !w.chainStarted {
w.chainStartDeposits = append(w.chainStartDeposits, depositData)
} else {
w.beaconDB.InsertPendingDeposit(w.ctx, deposit, big.NewInt(int64(depositLog.BlockNumber)))
}
log.WithFields(logrus.Fields{
"publicKey": fmt.Sprintf("%#x", depositInput.Pubkey),
"merkleTreeIndex": index,
}).Info("Deposit registered from deposit contract")
validDepositsCount.Inc()
} else {
log.WithFields(logrus.Fields{
"merkleTreeIndex": index,
}).Info("Invalid deposit registered in deposit contract")
}
}

View File

@@ -90,6 +90,7 @@ func TestProcessDepositLog_OK(t *testing.T) {
}
func TestProcessDepositLog_InsertsPendingDeposit(t *testing.T) {
hook := logTest.NewGlobal()
testAcc, err := setup()
if err != nil {
t.Fatalf("Unable to set up simulated backend %v", err)
@@ -124,10 +125,17 @@ func TestProcessDepositLog_InsertsPendingDeposit(t *testing.T) {
}
testAcc.txOpts.Value = amount32Eth
badData := []byte("bad data")
if _, err := testAcc.contract.Deposit(testAcc.txOpts, serializedData.Bytes()); err != nil {
t.Fatalf("Could not deposit to deposit contract %v", err)
}
// A deposit with bad data should also be correctly processed and added to the
// db in the pending deposits bucket.
if _, err := testAcc.contract.Deposit(testAcc.txOpts, badData); err != nil {
t.Fatalf("Could not deposit to deposit contract %v", err)
}
testAcc.backend.Commit()
query := ethereum.FilterQuery{
@@ -144,10 +152,13 @@ func TestProcessDepositLog_InsertsPendingDeposit(t *testing.T) {
web3Service.chainStarted = true
web3Service.ProcessDepositLog(logs[0])
web3Service.ProcessDepositLog(logs[1])
pendingDeposits := web3Service.beaconDB.PendingDeposits(context.Background(), nil /*blockNum*/)
if len(pendingDeposits) != 1 {
if len(pendingDeposits) != 2 {
t.Errorf("Unexpected number of deposits. Wanted 1 deposit, got %+v", pendingDeposits)
}
testutil.AssertLogsContain(t, hook, "Invalid deposit registered in deposit contract")
hook.Reset()
}
func TestUnpackDepositLogData_OK(t *testing.T) {
@@ -300,7 +311,7 @@ func TestProcessChainStartLog_8DuplicatePubkeys(t *testing.T) {
}
cachedDeposits := web3Service.ChainStartDeposits()
if len(cachedDeposits) != 1 {
if len(cachedDeposits) != depositsReqForChainStart {
t.Errorf(
"Did not cache the chain start deposits correctly, received %d, wanted %d",
len(cachedDeposits),