Skip to content

Commit

Permalink
Stable sorts transactions in the block in nonce order, enforce consen…
Browse files Browse the repository at this point in the history
…sus limits, induce voteBody and voteID transactions
  • Loading branch information
charithabandi committed Dec 13, 2024
1 parent 986beb2 commit c89986c
Show file tree
Hide file tree
Showing 22 changed files with 1,077 additions and 428 deletions.
9 changes: 5 additions & 4 deletions app/node/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func buildServer(ctx context.Context, d *coreDependencies) *server {
accounts := buildAccountStore(ctx, d, db)

// eventstore, votestore
_, vs := buildVoteStore(ctx, d, closers) // ev, vs
es, vs := buildVoteStore(ctx, d, closers) // ev, vs

// TxAPP
txApp := buildTxApp(ctx, d, db, accounts, vs, e)
Expand All @@ -80,7 +80,7 @@ func buildServer(ctx context.Context, d *coreDependencies) *server {
ss := buildSnapshotStore(d)

// BlockProcessor
bp := buildBlockProcessor(ctx, d, db, txApp, accounts, vs, ss)
bp := buildBlockProcessor(ctx, d, db, txApp, accounts, vs, ss, es)

// Consensus
ce := buildConsensusEngine(ctx, d, db, mp, bs, bp, valSet)
Expand Down Expand Up @@ -220,8 +220,9 @@ func buildTxApp(ctx context.Context, d *coreDependencies, db *pg.DB, accounts *a
return txapp
}

func buildBlockProcessor(ctx context.Context, d *coreDependencies, db *pg.DB, txapp *txapp.TxApp, accounts *accounts.Accounts, vs *voting.VoteStore, ss *snapshotter.SnapshotStore) *blockprocessor.BlockProcessor {
bp, err := blockprocessor.NewBlockProcessor(ctx, db, txapp, accounts, vs, ss, d.genesisCfg, d.logger.New("BP"))
func buildBlockProcessor(ctx context.Context, d *coreDependencies, db *pg.DB, txapp *txapp.TxApp, accounts *accounts.Accounts, vs *voting.VoteStore, ss *snapshotter.SnapshotStore, es *voting.EventStore) *blockprocessor.BlockProcessor {
signer := auth.GetNodeSigner(d.privKey)
bp, err := blockprocessor.NewBlockProcessor(ctx, db, txapp, accounts, vs, ss, es, d.genesisCfg, signer, d.logger.New("BP"))
if err != nil {
failBuild(err, "failed to create block processor")
}
Expand Down
16 changes: 9 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func DefaultConfig() *Config {
DiscoveryTimeout: Duration(30 * time.Second),
MaxRetries: 3,
},
Extensions: make(map[string]map[string]string),
}
}

Expand All @@ -170,13 +171,14 @@ type Config struct {
// ProfileMode string `toml:"profile_mode"`
// ProfileFile string `toml:"profile_file"`

P2P PeerConfig `toml:"p2p" comment:"P2P related configuration"`
Consensus ConsensusConfig `toml:"consensus" comment:"Consensus related configuration"`
DB DBConfig `toml:"db" comment:"DB (PostgreSQL) related configuration"`
RPC RPCConfig `toml:"rpc" comment:"User RPC service configuration"`
Admin AdminConfig `toml:"admin" comment:"Admin RPC service configuration"`
Snapshots SnapshotConfig `toml:"snapshots" comment:"Snapshot creation and provider configuration"`
StateSync StateSyncConfig `toml:"state_sync" comment:"Statesync configuration (vs block sync)"`
P2P PeerConfig `toml:"p2p" comment:"P2P related configuration"`
Consensus ConsensusConfig `toml:"consensus" comment:"Consensus related configuration"`
DB DBConfig `toml:"db" comment:"DB (PostgreSQL) related configuration"`
RPC RPCConfig `toml:"rpc" comment:"User RPC service configuration"`
Admin AdminConfig `toml:"admin" comment:"Admin RPC service configuration"`
Snapshots SnapshotConfig `toml:"snapshots" comment:"Snapshot creation and provider configuration"`
StateSync StateSyncConfig `toml:"state_sync" comment:"Statesync configuration (vs block sync)"`
Extensions map[string]map[string]string `toml:"extensions" comment:"extension configuration"`
}

// PeerConfig corresponds to the [peer] section of the config.
Expand Down
32 changes: 32 additions & 0 deletions core/types/payload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,35 @@ func TestMarshalUnmarshalPayload(t *testing.T) {

assert.Equal(t, tp.val, tp2.val)
}

func TestValidatorVoteBodyMarshalUnmarshal(t *testing.T) {
voteBody := &types.ValidatorVoteBodies{
Events: []*types.VotableEvent{
{
Type: "emptydata",
Body: []byte(""),
},
{
Type: "test",
Body: []byte("test"),
},
{
Type: "test2",
Body: []byte("random large data, random large data,random large data,random large data,random large data,random large data,random large data,random large data,random large data,random large data,random large data,random large data,random large data,"),
},
},
}

data, err := voteBody.MarshalBinary()
require.NoError(t, err)

voteBody2 := &types.ValidatorVoteBodies{}
err = voteBody2.UnmarshalBinary(data)
require.NoError(t, err)

require.NotNil(t, voteBody2)
require.NotNil(t, voteBody2.Events)
require.Len(t, voteBody2.Events, 3)

require.Equal(t, voteBody.Events, voteBody2.Events)
}
57 changes: 55 additions & 2 deletions core/types/payloads.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types

import (
"bytes"
"encoding"
"encoding/binary"
"errors"
Expand Down Expand Up @@ -606,15 +607,67 @@ type ValidatorVoteBodies struct {
var _ Payload = (*ValidatorVoteBodies)(nil)

func (v *ValidatorVoteBodies) MarshalBinary() ([]byte, error) {
return serialize.Encode(v)
buf := new(bytes.Buffer)
// Length of events (uint32)
if err := binary.Write(buf, binary.LittleEndian, uint32(len(v.Events))); err != nil {
return nil, err
}
for _, event := range v.Events {
// Length of event type (uint32)
if err := binary.Write(buf, binary.LittleEndian, uint32(len(event.Type))); err != nil {
return nil, err
}
// Event type
if _, err := buf.WriteString(event.Type); err != nil {
return nil, err
}
// Length of event body (uint32)
if err := binary.Write(buf, binary.LittleEndian, uint32(len(event.Body))); err != nil {
return nil, err
}
// Event body
if _, err := buf.Write(event.Body); err != nil {
return nil, err
}
}

return buf.Bytes(), nil
}

func (v *ValidatorVoteBodies) Type() PayloadType {
return PayloadTypeValidatorVoteBodies
}

func (v *ValidatorVoteBodies) UnmarshalBinary(p0 []byte) error {
return serialize.Decode(p0, v)
buf := bytes.NewBuffer(p0)
var numEvents uint32
if err := binary.Read(buf, binary.LittleEndian, &numEvents); err != nil {
return err
}
v.Events = make([]*VotableEvent, numEvents)
for i := range v.Events {
var eventTypeLen uint32
if err := binary.Read(buf, binary.LittleEndian, &eventTypeLen); err != nil {
return err
}
eventType := make([]byte, eventTypeLen)
if _, err := buf.Read(eventType); err != nil {
return err
}
var eventBodyLen uint32
if err := binary.Read(buf, binary.LittleEndian, &eventBodyLen); err != nil {
return err
}
eventBody := make([]byte, eventBodyLen)
if _, err := buf.Read(eventBody); err != nil {
return err
}
v.Events[i] = &VotableEvent{
Type: string(eventType),
Body: eventBody,
}
}
return nil
}

// CreateResolution is a payload for creating a new resolution.
Expand Down
Loading

0 comments on commit c89986c

Please sign in to comment.