Merge pull request #1626 from rlan35/general_fix

Verify sig every 100 blocks during state sync
pull/1631/head
Rongjian Lan 5 years ago committed by GitHub
commit e441239fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      api/service/explorer/service.go
  2. 3
      api/service/explorer/storage.go
  3. 8
      api/service/syncing/syncing.go

@ -641,6 +641,7 @@ func (s *ServiceAPI) GetExplorerCommittee(ctx context.Context, shardID uint32, e
func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
id := r.FormValue("id")
key := GetAddressKey(id)
txViewParam := r.FormValue("tx_view")
pageParam := r.FormValue("page")
offsetParam := r.FormValue("offset")
@ -697,9 +698,17 @@ func (s *Service) GetExplorerAddress(w http.ResponseWriter, r *http.Request) {
}
}
if balanceAddr.Cmp(big.NewInt(0)) != 0 {
data.Address.Balance = balanceAddr
db := s.Storage.GetDB()
bytes, err := db.Get([]byte(key))
if err = rlp.DecodeBytes(bytes, &data.Address); err != nil {
utils.Logger().Warn().Str("id", id).Msg("cannot convert data from DB")
w.WriteHeader(http.StatusInternalServerError)
return
}
data.Address.Balance = balanceAddr
switch txView {
case txViewNone:
data.Address.TXs = nil
@ -754,9 +763,20 @@ func (s *ServiceAPI) GetExplorerAddress(ctx context.Context, id, txView string,
}
}
if balanceAddr.Cmp(big.NewInt(0)) != 0 {
address.Balance = balanceAddr
key := GetAddressKey(id)
db := s.Service.Storage.GetDB()
bytes, err := db.Get([]byte(key))
if err != nil {
utils.Logger().Warn().Err(err).Str("id", id).Msg("cannot read address from db")
return address, nil
}
if err = rlp.DecodeBytes(bytes, &address); err != nil {
utils.Logger().Warn().Str("id", id).Msg("cannot convert data from DB")
return nil, err
}
address.Balance = balanceAddr
switch txView {
case txViewNone:
address.TXs = nil

@ -118,8 +118,7 @@ func (storage *Storage) Dump(block *types.Block, height uint64) {
explorerTransaction := GetTransaction(tx, block)
storage.UpdateTXStorage(batch, explorerTransaction, tx)
//storage.UpdateAddress(batch, explorerTransaction, tx)
storage.UpdateAddress(batch, explorerTransaction, tx)
}
if err := batch.Write(); err != nil {
ctxerror.Warn(utils.GetLogger(), err, "cannot write batch")

@ -546,9 +546,15 @@ func (ss *StateSync) updateBlockAndStatus(block *types.Block, bc *core.BlockChai
// Verify block signatures
// TODO chao: only when block is verified against last commit sigs, we can update the block and status
if block.NumberU64() > 1 {
err := bc.Engine().VerifyHeader(bc, block.Header(), true)
// Verify signature every 100 blocks
verifySig := block.NumberU64()%100 == 0
err := bc.Engine().VerifyHeader(bc, block.Header(), verifySig)
if err != nil {
utils.Logger().Error().Err(err).Msgf("[SYNC] failed verifying signatures for new block %d", block.NumberU64())
utils.Logger().Debug().Interface("block", bc.CurrentBlock()).Msg("[SYNC] Rolling back last 99 blocks!")
for i := 0; i < 99; i++ {
bc.Rollback([]common.Hash{bc.CurrentBlock().Hash()})
}
return false
}
}

Loading…
Cancel
Save