Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory and encoding fixes #1812

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mocks:
## generate protobuf files
protogen:
@echo "Generating protobuf files"
@find . -name '*.proto' -exec protoc --go_out=. --go_opt=paths=source_relative {} \;
@find . -name '*.proto' -exec protoc --go_out=. --go_opt=paths=source_relative --experimental_allow_proto3_optional {} \;

debug:
go build -gcflags=all="-N -l" -v -o build/bin/go-quai ./cmd/go-quai
Expand Down
4 changes: 2 additions & 2 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: 2 additions & 2 deletions core/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ func DefaultGardenGenesisBlock(consensusEngine string) *Genesis {
Config: params.Blake3PowGardenChainConfig,
Nonce: 66,
ExtraData: hexutil.MustDecode("0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa"),
GasLimit: 40000000,
Difficulty: big.NewInt(4000000),
GasLimit: 160000000,
Difficulty: big.NewInt(1000000),
}
}
return &Genesis{
Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/db.pb.go

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

2 changes: 1 addition & 1 deletion core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ func (pool *TxPool) add(tx *types.Transaction, local bool) (replaced bool, err e
}
// Mark local addresses and journal local transactions
if local && !pool.locals.contains(internal) {
pool.logger.WithField("address", from).Info("Setting new local account")
pool.logger.WithField("address", from).Debug("Setting new local account")
pool.locals.add(internal)
pool.priced.Removed(pool.all.RemoteToLocals(pool.locals)) // Migrate the remotes if it's marked as local first time.
}
Expand Down
441 changes: 207 additions & 234 deletions core/types/proto_block.pb.go

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions core/types/proto_block.proto
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,11 @@ message ProtoWorkObject {
message ProtoWorkObjects { repeated ProtoWorkObject work_objects = 1; }

message ProtoWorkObjectBlockView {
optional ProtoWorkObjectHeader wo_header = 1;
optional ProtoWorkObjectBody wo_body = 2;
optional ProtoWorkObject work_object = 1;
}

message ProtoWorkObjectHeaderView {
optional ProtoWorkObjectHeader wo_header = 1;
optional ProtoWorkObjectBody wo_body = 2;
optional ProtoWorkObject work_object = 1;
}

message ProtoAccessTuple {
Expand Down
45 changes: 17 additions & 28 deletions core/types/wo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"math/big"
"sync"
"sync/atomic"
"time"

Expand All @@ -14,6 +15,12 @@ import (
"lukechampine.com/blake3"
)

var ObjectPool = sync.Pool{
New: func() interface{} {
return new(interface{})
},
}

type WorkObject struct {
woHeader *WorkObjectHeader
woBody *WorkObjectBody
Expand Down Expand Up @@ -760,60 +767,42 @@ func (wo *WorkObject) ProtoEncode(woType WorkObjectView) (*ProtoWorkObject, erro
}

func (wo *WorkObjectHeaderView) ProtoEncode() (*ProtoWorkObjectHeaderView, error) {
header, err := wo.woHeader.ProtoEncode()
if err != nil {
return nil, err
}
body, err := wo.woBody.ProtoEncode()
protoWo, err := wo.WorkObject.ProtoEncode(HeaderObject)
if err != nil {
return nil, err
}
return &ProtoWorkObjectHeaderView{
WoHeader: header,
WoBody: body,
WorkObject: protoWo,
}, nil
}

func (wo *WorkObjectBlockView) ProtoEncode() (*ProtoWorkObjectBlockView, error) {
header, err := wo.woHeader.ProtoEncode()
if err != nil {
return nil, err
}
body, err := wo.woBody.ProtoEncode()
protoWo, err := wo.WorkObject.ProtoEncode(BlockObject)
if err != nil {
return nil, err
}
return &ProtoWorkObjectBlockView{
WoHeader: header,
WoBody: body,
WorkObject: protoWo,
}, nil
}

func (wo *WorkObjectHeaderView) ProtoDecode(data *ProtoWorkObjectHeaderView, location common.Location) error {
wo.woHeader = new(WorkObjectHeader)
err := wo.woHeader.ProtoDecode(data.GetWoHeader())
if err != nil {
return err
}
wo.woBody = new(WorkObjectBody)
err = wo.woBody.ProtoDecode(data.GetWoBody(), location, BlockObject)
decodeWo := new(WorkObject)
err := decodeWo.ProtoDecode(data.GetWorkObject(), location, HeaderObject)
if err != nil {
return err
}
wo.WorkObject = decodeWo
return nil
}

func (wob *WorkObjectBlockView) ProtoDecode(data *ProtoWorkObjectBlockView, location common.Location) error {
wob.woHeader = new(WorkObjectHeader)
err := wob.woHeader.ProtoDecode(data.GetWoHeader())
if err != nil {
return err
}
wob.woBody = new(WorkObjectBody)
err = wob.woBody.ProtoDecode(data.GetWoBody(), location, BlockObject)
decodeWo := new(WorkObject)
err := decodeWo.ProtoDecode(data.GetWorkObject(), location, BlockObject)
if err != nil {
return err
}
wob.WorkObject = decodeWo
return nil
}

Expand Down
6 changes: 2 additions & 4 deletions p2p/node/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,8 @@ func (p *P2PNode) GetTrieNode(hash common.Hash, location common.Location) *trie.
}

func (p *P2PNode) handleBroadcast(sourcePeer peer.ID, topic string, data interface{}, nodeLocation common.Location) {
switch v := data.(type) {
case types.WorkObject:
p.cacheAdd(v.Hash(), &v, nodeLocation)
// TODO: send it to consensus
switch data.(type) {
case types.WorkObjectBlockView:
case types.WorkObjectHeaderView:
case types.Transactions:
case types.WorkObjectHeader:
Expand Down
4 changes: 2 additions & 2 deletions p2p/node/peerManager/peerdb/peer_info.pb.go

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

4 changes: 3 additions & 1 deletion p2p/node/pubsubManager/gossipsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/dominant-strategies/go-quai/cmd/utils"
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/core/types"
"github.com/dominant-strategies/go-quai/log"
"github.com/dominant-strategies/go-quai/p2p/pb"
"github.com/dominant-strategies/go-quai/quai"
Expand Down Expand Up @@ -145,7 +146,8 @@ func (g *PubsubManager) Subscribe(location common.Location, datatype interface{}
go msgWorker(location) // If this worker exits, start a new one
}()
for msg := range msgChan { // This should exit when msgChan is closed
var data interface{}
data := types.ObjectPool.Get()
data = nil
// unmarshal the received data depending on the topic's type
err = pb.UnmarshalAndConvert(msg.Data, location, &data, datatype)
if err != nil {
Expand Down
35 changes: 14 additions & 21 deletions p2p/pb/proto_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,79 +270,72 @@ func ConvertAndMarshal(data interface{}) ([]byte, error) {
func UnmarshalAndConvert(data []byte, sourceLocation common.Location, dataPtr *interface{}, datatype interface{}) error {
switch datatype.(type) {
case *types.WorkObjectBlockView:
protoWorkObject := &types.ProtoWorkObject{}
protoWorkObject := &types.ProtoWorkObjectBlockView{}
err := proto.Unmarshal(data, protoWorkObject)
if err != nil {
return err
}
workObject := &types.WorkObject{}
if protoWorkObject.WoHeader == nil {
return errors.New("woheader is nil")
}
if protoWorkObject.WoHeader.Location == nil {
return errors.New("location is nil")
}
err = workObject.ProtoDecode(protoWorkObject, sourceLocation, types.BlockObject)

workObjectBlockView := types.WorkObjectBlockView{}
workObjectBlockView.WorkObject = &types.WorkObject{}
err = workObjectBlockView.ProtoDecode(protoWorkObject, sourceLocation)
if err != nil {
return err
}
*dataPtr = *workObject
*dataPtr = workObjectBlockView
return nil
case *types.WorkObjectHeaderView:
protoWorkObject := &types.ProtoWorkObjectHeaderView{}
err := proto.Unmarshal(data, protoWorkObject)
if err != nil {
return err
}
workObjectHeaderView := &types.WorkObjectHeaderView{}
if protoWorkObject.WoHeader.Location == nil {
return errors.New("location is nil")
}
workObjectHeaderView := types.WorkObjectHeaderView{}
workObjectHeaderView.WorkObject = &types.WorkObject{}
err = workObjectHeaderView.ProtoDecode(protoWorkObject, sourceLocation)
if err != nil {
return err
}
*dataPtr = *workObjectHeaderView
*dataPtr = workObjectHeaderView
return nil
case *types.WorkObjectHeader:
protoWorkObjectHeader := &types.ProtoWorkObjectHeader{}
err := proto.Unmarshal(data, protoWorkObjectHeader)
if err != nil {
return err
}
workObjectHeader := &types.WorkObjectHeader{}
workObjectHeader := types.WorkObjectHeader{}
err = workObjectHeader.ProtoDecode(protoWorkObjectHeader)
if err != nil {
return err
}
*dataPtr = *workObjectHeader
*dataPtr = workObjectHeader
return nil
case *types.Header:
protoHeader := &types.ProtoHeader{}
err := proto.Unmarshal(data, protoHeader)
if err != nil {
return err
}
header := &types.Header{}
header := types.Header{}
err = header.ProtoDecode(protoHeader, sourceLocation)
if err != nil {
return err
}
*dataPtr = *header
*dataPtr = header
return nil
case *types.Transactions:
protoTransactions := &types.ProtoTransactions{}
err := proto.Unmarshal(data, protoTransactions)
if err != nil {
return err
}
transactions := &types.Transactions{}
transactions := types.Transactions{}
err = transactions.ProtoDecode(protoTransactions, sourceLocation)
if err != nil {
return err
}
*dataPtr = *transactions
*dataPtr = transactions
return nil
case common.Hash:
protoHash := &common.ProtoHash{}
Expand Down
8 changes: 2 additions & 6 deletions p2p/pb/quai_messages.pb.go

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

2 changes: 1 addition & 1 deletion params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var (

// Blake3GenesisHashes
Blake3PowColosseumGenesisHash = common.HexToHash("0x0ca6ff4426eab568de6773fb21d623d3fe6f2f0d2572c0b0b978384373e0895d")
Blake3PowGardenGenesisHash = common.HexToHash("0x43af9e6e91fbf408b9c6fd423ed34e5ad0bd2154737cefc204d145ffc660fc9c")
Blake3PowGardenGenesisHash = common.HexToHash("0x02874a9e708fd75a6ed73bf1d6dbc275a87b8914c3f57da0bbdc959c381d0f67")
Blake3PowOrchardGenesisHash = common.HexToHash("0x43af9e6e91fbf408b9c6fd423ed34e5ad0bd2154737cefc204d145ffc660fc9c")
Blake3PowLocalGenesisHash = common.HexToHash("0xdb2e0d01d953a3df18eb42c26cf8caeb58a905474ffe391bc21354ead6e2e0b3")
Blake3PowLighthouseGenesisHash = common.HexToHash("0x43af9e6e91fbf408b9c6fd423ed34e5ad0bd2154737cefc204d145ffc660fc9c")
Expand Down
4 changes: 2 additions & 2 deletions params/protocol_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const (
GasLimitStepOneBlockThreshold uint64 = 150000
GasLimitStepTwoBlockThreshold uint64 = 300000
GasLimitStepThreeBlockThreshold uint64 = 450000
MinGasLimit uint64 = 5000000 // Minimum the gas limit may ever be.
GenesisGasLimit uint64 = 5000000 // Gas limit of the Genesis block.
MinGasLimit uint64 = 160000000 // Minimum the gas limit may ever be.
GenesisGasLimit uint64 = 5000000 // Gas limit of the Genesis block.

MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.
Expand Down
7 changes: 4 additions & 3 deletions quai/p2p_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ func (qbe *QuaiBackend) GetBackend(location common.Location) *quaiapi.Backend {

// Handle consensus data propagated to us from our peers
func (qbe *QuaiBackend) OnNewBroadcast(sourcePeer p2p.PeerID, topic string, data interface{}, nodeLocation common.Location) bool {
defer types.ObjectPool.Put(data)
switch data := data.(type) {
case types.WorkObject:
case types.WorkObjectBlockView:
backend := *qbe.GetBackend(nodeLocation)
if backend == nil {
log.Global.Error("no backend found")
Expand All @@ -88,7 +89,7 @@ func (qbe *QuaiBackend) OnNewBroadcast(sourcePeer p2p.PeerID, topic string, data
// TODO: Verify the Block before writing it
// TODO: Determine if the block information was lively or stale and rate
// the peer accordingly
backend.WriteBlock(&data)
backend.WriteBlock(data.WorkObject)
// If it was a good broadcast, mark the peer as lively
qbe.p2pBackend.MarkLivelyPeer(sourcePeer, topic)
case types.WorkObjectHeaderView:
Expand All @@ -99,7 +100,7 @@ func (qbe *QuaiBackend) OnNewBroadcast(sourcePeer p2p.PeerID, topic string, data
}
// Only append this in the case of the slice
if !backend.ProcessingState() && backend.NodeCtx() == common.ZONE_CTX {
backend.WriteBlock(data.ConvertToBlockView().WorkObject)
backend.WriteBlock(data.WorkObject)
}
// If it was a good broadcast, mark the peer as lively
qbe.p2pBackend.MarkLivelyPeer(sourcePeer, topic)
Expand Down
2 changes: 1 addition & 1 deletion rpc/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func (h *handler) handleCallMsg(ctx *callProc, msg *jsonrpcMessage) *jsonrpcMess
if resp.Error.Data != nil {
ctx = append(ctx, "errdata", resp.Error.Data)
}
h.log.Warn(ctx, "Served "+msg.Method)
h.log.Debug(ctx, "Served "+msg.Method)
} else {
h.log.Debug("Served " + msg.Method)
}
Expand Down
4 changes: 2 additions & 2 deletions trie/proto_trienode.pb.go

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

Loading