Skip to content

Commit

Permalink
write total difficulty (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhd-vu authored Jun 9, 2023
1 parent 19c9659 commit 881a90b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
3 changes: 2 additions & 1 deletion p2p/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"context"
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand All @@ -13,7 +14,7 @@ import (
// to. To use another database solution, just implement these methods and
// update the sensor to use the new connection.
type Database interface {
WriteBlock(context.Context, *enode.Node, *types.Block)
WriteBlock(context.Context, *enode.Node, *types.Block, *big.Int)
WriteBlockHeaders(context.Context, []*types.Header)
WriteBlockHashes(context.Context, *enode.Node, []common.Hash)
WriteBlockBody(context.Context, *eth.BlockBody, common.Hash)
Expand Down
22 changes: 19 additions & 3 deletions p2p/database/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package database
import (
"context"
"fmt"
"math/big"
"time"

"cloud.google.com/go/datastore"
Expand Down Expand Up @@ -66,8 +67,9 @@ type DatastoreHeader struct {
// DatastoreBlock represents a block stored in datastore.
type DatastoreBlock struct {
*DatastoreHeader
Transactions []*datastore.Key
Uncles []*datastore.Key
TotalDifficulty string
Transactions []*datastore.Key
Uncles []*datastore.Key
}

// DatastoreTransaction represents a transaction stored in datastore. Data is
Expand Down Expand Up @@ -107,7 +109,7 @@ func NewDatastore(ctx context.Context, projectID string, sensorID string, maxCon
}

// WriteBlock writes the block and the block event to datastore.
func (d *datastoreWrapper) WriteBlock(ctx context.Context, peer *enode.Node, block *types.Block) {
func (d *datastoreWrapper) WriteBlock(ctx context.Context, peer *enode.Node, block *types.Block, td *big.Int) {
d.writeEvent(peer, blockEventsKind, block.Hash(), blocksKind)

key := datastore.NameKey(blocksKind, block.Hash().Hex(), nil)
Expand All @@ -116,11 +118,20 @@ func (d *datastoreWrapper) WriteBlock(ctx context.Context, peer *enode.Node, blo
// are nil we will just set them.
_ = d.client.Get(ctx, key, &dsBlock)

shouldWrite := false

if dsBlock.DatastoreHeader == nil {
shouldWrite = true
dsBlock.DatastoreHeader = newDatastoreHeader(block.Header())
}

if len(dsBlock.TotalDifficulty) == 0 {
shouldWrite = true
dsBlock.TotalDifficulty = td.String()
}

if dsBlock.Transactions == nil && len(block.Transactions()) > 0 {
shouldWrite = true
if d.shouldWriteTransactions {
d.writeTransactions(ctx, block.Transactions())
}
Expand All @@ -132,13 +143,18 @@ func (d *datastoreWrapper) WriteBlock(ctx context.Context, peer *enode.Node, blo
}

if dsBlock.Uncles == nil && len(block.Uncles()) > 0 {
shouldWrite = true
dsBlock.Uncles = make([]*datastore.Key, 0, len(block.Uncles()))
for _, uncle := range block.Uncles() {
d.writeBlockHeader(ctx, uncle)
dsBlock.Uncles = append(dsBlock.Uncles, datastore.NameKey(blocksKind, uncle.Hash().Hex(), nil))
}
}

if !shouldWrite {
return
}

if _, err := d.client.Put(ctx, key, &dsBlock); err != nil {
log.Error().Err(err).Msg("Failed to write new block")
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/rlpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (c *Conn) ReadAndServe(db database.Database, count *MessageCount) error {

dbCh <- struct{}{}
go func() {
db.WriteBlock(ctx, c.node, msg.Block)
db.WriteBlock(ctx, c.node, msg.Block, msg.TD)
<-dbCh
}()
}
Expand Down

0 comments on commit 881a90b

Please sign in to comment.