Skip to content

Commit

Permalink
Made every accessor in rawdb use protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
gameofpointers committed Feb 21, 2024
1 parent 9a6fa6b commit 258afcf
Show file tree
Hide file tree
Showing 33 changed files with 2,729 additions and 744 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ mocks:
## generate protobuf files
protogen:
@echo "Generating protobuf files"
@protoc --go_out=. --go_opt=paths=source_relative \
./**/*.proto
@find . -name '*.proto' -exec protoc --go_out=. --go_opt=paths=source_relative {} \;

debug:
go build -gcflags=all="-N -l" -v -o build/bin/go-quai ./cmd/go-quai
Expand Down
19 changes: 19 additions & 0 deletions common/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ func NewAddressFromData(inner AddressData) Address {
return Address{inner: inner}
}

// ProtoEncode converts the address to a protobuf representation.
func (a Address) ProtoEncode() *ProtoAddress {
if a.inner == nil {
return nil
}
protoAddress := &ProtoAddress{}
protoAddress.Value = a.inner.Bytes()
return protoAddress
}

// ProtoDecode converts the protobuf to an address representation.
func (a *Address) ProtoDecode(protoAddress *ProtoAddress, location Location) error {
if protoAddress.Value == nil {
return errors.New("address is nil in ProtoDecode")
}
*a = BytesToAddress(protoAddress.GetValue(), location)
return nil
}

// EncodeRLP serializes b into the Quai RLP block format.
func (a Address) EncodeRLP(w io.Writer) error {
if a.inner == nil {
Expand Down
145 changes: 136 additions & 9 deletions common/proto_common.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions common/proto_common.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ option go_package = "github.com/dominant-strategies/go-quai/common";
message ProtoLocation { bytes value = 1; }

message ProtoHash { bytes value = 1; }

message ProtoHashes { repeated ProtoHash hashes = 1; }

message ProtoAddress { bytes value = 1; }
19 changes: 19 additions & 0 deletions common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,25 @@ func (h UnprefixedHash) MarshalText() ([]byte, error) {
return []byte(hex.EncodeToString(h[:])), nil
}

// Hashes is a slice of Hash
type Hashes []Hash

func (h Hashes) ProtoEncode() *ProtoHashes {
res := make([]*ProtoHash, len(h))
for i, hash := range h {
res[i] = hash.ProtoEncode()
}
return &ProtoHashes{Hashes: res}
}

func (h *Hashes) ProtoDecode(hashes *ProtoHashes) {
res := make([]Hash, len(hashes.GetHashes()))
for i, hash := range hashes.GetHashes() {
res[i].ProtoDecode(hash)
}
*h = res
}

/////////// Address

type addrPrefixRange struct {
Expand Down
20 changes: 10 additions & 10 deletions core/bodydb.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ type BodyDb struct {
blockProcFeed event.Feed
scope event.SubscriptionScope

engine consensus.Engine
chainmu sync.RWMutex
blockCache *lru.Cache
bodyCache *lru.Cache
bodyRLPCache *lru.Cache
processor *StateProcessor
engine consensus.Engine
chainmu sync.RWMutex
blockCache *lru.Cache
bodyCache *lru.Cache
bodyProtoCache *lru.Cache
processor *StateProcessor

slicesRunning []common.Location

Expand All @@ -67,14 +67,14 @@ func NewBodyDb(db ethdb.Database, engine consensus.Engine, hc *HeaderChain, chai
bodyRLPCache, _ := lru.New(bodyCacheLimit)
bc.blockCache = blockCache
bc.bodyCache = bodyCache
bc.bodyRLPCache = bodyRLPCache
bc.bodyProtoCache = bodyRLPCache
} else {
blockCache, _ := lru.New(10)
bodyCache, _ := lru.New(10)
bodyRLPCache, _ := lru.New(10)
bc.blockCache = blockCache
bc.bodyCache = bodyCache
bc.bodyRLPCache = bodyRLPCache
bc.bodyProtoCache = bodyRLPCache
}

// only start the state processor in zone
Expand Down Expand Up @@ -161,7 +161,7 @@ func (bc *BodyDb) GetBlock(hash common.Hash, number uint64) *types.Block {
if block, ok := bc.blockCache.Get(hash); ok {
return block.(*types.Block)
}
block := rawdb.ReadBlock(bc.db, hash, number)
block := rawdb.ReadBlock(bc.db, hash, number, bc.NodeLocation())
if block == nil {
return nil
}
Expand All @@ -173,7 +173,7 @@ func (bc *BodyDb) GetBlock(hash common.Hash, number uint64) *types.Block {
// GetBlockOrCandidate retrieves any known block from the database by hash and number,
// caching it if found.
func (bc *BodyDb) GetBlockOrCandidate(hash common.Hash, number uint64) *types.Block {
block := rawdb.ReadBlock(bc.db, hash, number)
block := rawdb.ReadBlock(bc.db, hash, number, bc.NodeLocation())
if block == nil {
return nil
}
Expand Down
14 changes: 7 additions & 7 deletions core/headerchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ func (hc *HeaderChain) SetCurrentState(head *types.Header) error {
}
// Checking of the Etx set exists makes sure that we have processed the
// state of the parent block
etxSet := rawdb.ReadEtxSet(hc.headerDb, header.Hash(), header.NumberU64(nodeCtx))
etxSet := rawdb.ReadEtxSet(hc.headerDb, header.Hash(), header.NumberU64(nodeCtx), hc.NodeLocation())
if etxSet != nil {
break
}
Expand Down Expand Up @@ -457,7 +457,7 @@ func (hc *HeaderChain) ReadInboundEtxsAndAppendBlock(header *types.Header) error
}
var inboundEtxs types.Transactions
if order < nodeCtx {
inboundEtxs = rawdb.ReadInboundEtxs(hc.headerDb, header.Hash())
inboundEtxs = rawdb.ReadInboundEtxs(hc.headerDb, header.Hash(), hc.NodeLocation())
}
return hc.AppendBlock(block, inboundEtxs)
}
Expand Down Expand Up @@ -547,7 +547,7 @@ func (hc *HeaderChain) Stop() {
return
}

hashes := make([]common.Hash, 0)
hashes := make(common.Hashes, 0)
for i := 0; i < len(hc.heads); i++ {
hashes = append(hashes, hc.heads[i].Hash())
}
Expand Down Expand Up @@ -929,7 +929,7 @@ func (hc *HeaderChain) GetBody(hash common.Hash) *types.Body {
if number == nil {
return nil
}
body := rawdb.ReadBody(hc.headerDb, hash, *number)
body := rawdb.ReadBody(hc.headerDb, hash, *number, hc.NodeLocation())
if body == nil {
return nil
}
Expand All @@ -942,19 +942,19 @@ func (hc *HeaderChain) GetBody(hash common.Hash) *types.Body {
// caching it if found.
func (hc *HeaderChain) GetBodyRLP(hash common.Hash) rlp.RawValue {
// Short circuit if the body's already in the cache, retrieve otherwise
if cached, ok := hc.bc.bodyRLPCache.Get(hash); ok {
if cached, ok := hc.bc.bodyProtoCache.Get(hash); ok {
return cached.(rlp.RawValue)
}
number := hc.GetBlockNumber(hash)
if number == nil {
return nil
}
body := rawdb.ReadBodyRLP(hc.headerDb, hash, *number)
body := rawdb.ReadBodyProto(hc.headerDb, hash, *number)
if len(body) == 0 {
return nil
}
// Cache the found body for next time and return
hc.bc.bodyRLPCache.Add(hash, body)
hc.bc.bodyProtoCache.Add(hash, body)
return body
}

Expand Down
Loading

0 comments on commit 258afcf

Please sign in to comment.