Skip to content

Commit

Permalink
feat(sync): define full and prune service (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
b00f authored Jul 13, 2024
1 parent 456cc09 commit 89ba289
Show file tree
Hide file tree
Showing 36 changed files with 345 additions and 643 deletions.
6 changes: 3 additions & 3 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,17 +393,17 @@ func StartNode(workingDir string, passwordFetcher func(*wallet.Wallet) (string,
return nil, nil, err
}

nodeInstance, err := node.NewNode(gen, conf, valKeys, rewardAddrs)
nd, err := node.NewNode(gen, conf, valKeys, rewardAddrs)
if err != nil {
return nil, nil, err
}

err = nodeInstance.Start()
err = nd.Start()
if err != nil {
return nil, nil, err
}

return nodeInstance, walletInstance, nil
return nd, walletInstance, nil
}

// makeLocalGenesis makes genesis file for the local network.
Expand Down
8 changes: 4 additions & 4 deletions cmd/gtk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
log.Println("application startup")
})

n, wlt, err := newNode(workingDir)
nd, wlt, err := newNode(workingDir)
fatalErrorCheck(err)

// Connect function to application activate event
Expand All @@ -111,7 +111,7 @@ func main() {

// Running the run-up logic in a separate goroutine
glib.TimeoutAdd(uint(100), func() bool {
run(n, wlt, app)
run(nd, wlt, app)
splashDlg.Destroy()

// Ensures the function is not called again
Expand All @@ -122,14 +122,14 @@ func main() {
// Connect function to application shutdown event, this is not required.
app.Connect("shutdown", func() {
log.Println("Application shutdown")
n.Stop()
nd.Stop()
_ = fileLock.Unlock()
})

cmd.TrapSignal(func() {
cmd.PrintInfoMsgf("Exiting...")

n.Stop()
nd.Stop()
_ = fileLock.Unlock()
})

Expand Down
4 changes: 0 additions & 4 deletions config/example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,6 @@
# Default is `10s`.
session_timeout = "10s"

# `node_network` indicates whether the node is capable of serving the complete blockchain.
# Default is `true`.
node_network = true

# `sync.firewall` contains configuration options for the sync firewall.
[sync.firewall]
# `banned_nets` contains the list of IPs and subnets that should be banned.
Expand Down
4 changes: 4 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/pactus-project/pactus/store"
"github.com/pactus-project/pactus/sync"
"github.com/pactus-project/pactus/sync/bundle/message"
"github.com/pactus-project/pactus/sync/peerset/peer/service"
"github.com/pactus-project/pactus/txpool"
"github.com/pactus-project/pactus/util"
"github.com/pactus-project/pactus/util/logger"
Expand Down Expand Up @@ -79,6 +80,9 @@ func NewNode(genDoc *genesis.Genesis, conf *config.Config,
consMgr := consensus.NewManager(conf.Consensus, st, valKeys, rewardAddrs, messageCh)
walletMgr := wallet.NewWalletManager(conf.WalletManager)

if !str.IsPruned() {
conf.Sync.Services.Append(service.FullNode)
}
syn, err := sync.NewSynchronizer(conf.Sync, valKeys, st, consMgr, net, messageCh)
if err != nil {
return nil, err
Expand Down
10 changes: 6 additions & 4 deletions node/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ func TestRunningNode(t *testing.T) {

valKeys := []*bls.ValidatorKey{ts.RandValKey(), ts.RandValKey()}
rewardAddrs := []crypto.Address{ts.RandAccAddress(), ts.RandAccAddress()}
n, err := NewNode(gen, conf, valKeys, rewardAddrs)
nd, err := NewNode(gen, conf, valKeys, rewardAddrs)
assert.True(t, conf.Sync.Services.IsFullNode())
assert.True(t, conf.Sync.Services.IsPrunedNode())

require.NoError(t, err)
assert.Equal(t, n.state.LastBlockHash(), hash.UndefHash)
assert.Equal(t, nd.state.LastBlockHash(), hash.UndefHash)

err = n.Start()
err = nd.Start()
require.NoError(t, err)
n.Stop()
nd.Stop()
}
2 changes: 1 addition & 1 deletion sync/bundle/message/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type HelloMessage struct {
}

func NewHelloMessage(pid peer.ID, moniker string,
height uint32, services service.Services, blockHash, genesisHash hash.Hash,
services service.Services, height uint32, blockHash, genesisHash hash.Hash,
) *HelloMessage {
return &HelloMessage{
PeerID: pid,
Expand Down
17 changes: 12 additions & 5 deletions sync/bundle/message/hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/pactus-project/pactus/crypto"
"github.com/pactus-project/pactus/crypto/bls"
"github.com/pactus-project/pactus/sync/peerset/peer/service"
"github.com/pactus-project/pactus/util/errors"
"github.com/pactus-project/pactus/util/testsuite"
"github.com/stretchr/testify/assert"
Expand All @@ -21,7 +22,8 @@ func TestHelloMessage(t *testing.T) {

t.Run("Invalid signature", func(t *testing.T) {
valKey := ts.RandValKey()
m := NewHelloMessage(ts.RandPeerID(), "Oscar", 100, 0, ts.RandHash(), ts.RandHash())
m := NewHelloMessage(ts.RandPeerID(), "Oscar", service.New(service.FullNode),
ts.RandHeight(), ts.RandHash(), ts.RandHash())
m.Sign([]*bls.ValidatorKey{valKey})
m.Signature = ts.RandBLSSignature()

Expand All @@ -30,7 +32,8 @@ func TestHelloMessage(t *testing.T) {

t.Run("Signature is nil", func(t *testing.T) {
valKey := ts.RandValKey()
m := NewHelloMessage(ts.RandPeerID(), "Oscar", 100, 0, ts.RandHash(), ts.RandHash())
m := NewHelloMessage(ts.RandPeerID(), "Oscar", service.New(service.FullNode),
ts.RandHeight(), ts.RandHash(), ts.RandHash())
m.Sign([]*bls.ValidatorKey{valKey})
m.Signature = nil

Expand All @@ -39,7 +42,8 @@ func TestHelloMessage(t *testing.T) {

t.Run("PublicKeys are empty", func(t *testing.T) {
valKey := ts.RandValKey()
m := NewHelloMessage(ts.RandPeerID(), "Oscar", 100, 0, ts.RandHash(), ts.RandHash())
m := NewHelloMessage(ts.RandPeerID(), "Oscar", service.New(service.FullNode),
ts.RandHeight(), ts.RandHash(), ts.RandHash())
m.Sign([]*bls.ValidatorKey{valKey})
m.PublicKeys = make([]*bls.PublicKey, 0)

Expand All @@ -50,18 +54,21 @@ func TestHelloMessage(t *testing.T) {
time1 := time.Now()
myTimeUnixMilli := time1.UnixMilli()

m := NewHelloMessage(ts.RandPeerID(), "Alice", 100, 0, ts.RandHash(), ts.RandHash())
m := NewHelloMessage(ts.RandPeerID(), "Alice", service.New(service.FullNode),
ts.RandHeight(), ts.RandHash(), ts.RandHash())

assert.LessOrEqual(t, m.MyTimeUnixMilli, time.Now().UnixMilli())
assert.GreaterOrEqual(t, m.MyTimeUnixMilli, myTimeUnixMilli)
})

t.Run("Ok", func(t *testing.T) {
valKey := ts.RandValKey()
m := NewHelloMessage(ts.RandPeerID(), "Alice", 100, 0, ts.RandHash(), ts.RandHash())
m := NewHelloMessage(ts.RandPeerID(), "Alice", service.New(service.FullNode),
ts.RandHeight(), ts.RandHash(), ts.RandHash())
m.Sign([]*bls.ValidatorKey{valKey})

assert.NoError(t, m.BasicCheck())
assert.Contains(t, m.String(), "Alice")
assert.Contains(t, m.String(), "FULL")
})
}
23 changes: 7 additions & 16 deletions sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ import (
type Config struct {
Moniker string `toml:"moniker"`
SessionTimeout time.Duration `toml:"session_timeout"`
NodeNetwork bool `toml:"node_network"`
Firewall *firewall.Config `toml:"firewall"`

// Private configs
MaxSessions int `toml:"-"`
LatestBlockInterval uint32 `toml:"-"`
BlockPerMessage uint32 `toml:"-"`
LatestSupportingVer version.Version `toml:"-"`
MaxSessions int `toml:"-"`
LatestBlockInterval uint32 `toml:"-"`
BlockPerMessage uint32 `toml:"-"`
LatestSupportingVer version.Version `toml:"-"`
Services service.Services `toml:"-"`
}

func DefaultConfig() *Config {
return &Config{
SessionTimeout: time.Second * 10,
NodeNetwork: true,
Services: service.New(service.PrunedNode),
BlockPerMessage: 60,
MaxSessions: 8,
LatestBlockInterval: 720,
LatestBlockInterval: 10 * 8640, // 10 days, same as default retention blocks in prune node
Firewall: firewall.DefaultConfig(),
LatestSupportingVer: version.Version{
Major: 1,
Expand All @@ -47,12 +47,3 @@ func (conf *Config) CacheSize() int {
return util.LogScale(
int(conf.BlockPerMessage * conf.LatestBlockInterval))
}

func (conf *Config) Services() service.Services {
s := service.New()
if conf.NodeNetwork {
s.Append(service.Network)
}

return s
}
2 changes: 1 addition & 1 deletion sync/handler_blocks_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (handler *blocksRequestHandler) ParseMessage(m message.Message, pid peer.ID
}

ourHeight := handler.state.LastBlockHeight()
if !handler.config.NodeNetwork {
if !handler.config.Services.IsFullNode() {
if ourHeight > handler.config.LatestBlockInterval && msg.From < ourHeight-handler.config.LatestBlockInterval {
response := message.NewBlocksResponseMessage(message.ResponseCodeRejected,
fmt.Sprintf("the request height is not acceptable: %v", msg.From), msg.SessionID, 0, nil, nil)
Expand Down
23 changes: 14 additions & 9 deletions sync/handler_blocks_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
)

func TestBlocksRequestMessages(t *testing.T) {
config := testConfig()
config.NodeNetwork = false
t.Run("NetworkLimited service is enabled", func(t *testing.T) {
config := testConfig()
config.Services = service.Services(service.PrunedNode)

td := setup(t, config)
sid := td.RandInt(100)
td := setup(t, config)
sid := td.RandInt(100)

td.state.CommitTestBlocks(31)

t.Run("NodeNetwork flag is not set", func(t *testing.T) {
td.state.CommitTestBlocks(31)
curHeight := td.state.LastBlockHeight()

t.Run("Reject request from unknown peers", func(t *testing.T) {
Expand Down Expand Up @@ -118,8 +117,14 @@ func TestBlocksRequestMessages(t *testing.T) {
})
})

t.Run("NodeNetwork flag set", func(t *testing.T) {
td.sync.config.NodeNetwork = true
t.Run("Network service is enabled", func(t *testing.T) {
config := testConfig()
config.Services = service.New(service.FullNode)

td := setup(t, config)
sid := td.RandInt(100)

td.state.CommitTestBlocks(31)
pid := td.addPeer(t, status.StatusKnown, service.New(service.None))

t.Run("Requesting one block", func(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion sync/handler_blocks_response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ func makeAliceAndBobNetworks(t *testing.T) *networkAliceBob {
networkAlice := network.MockingNetwork(ts, ts.RandPeerID())
networkBob := network.MockingNetwork(ts, ts.RandPeerID())

configBob.NodeNetwork = true
networkAlice.AddAnotherNetwork(networkBob)
networkBob.AddAnotherNetwork(networkAlice)

Expand Down
40 changes: 18 additions & 22 deletions sync/handler_hello_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func TestParsingHelloMessages(t *testing.T) {
func(t *testing.T) {
valKey := td.RandValKey()
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "unknown-peer", 0, 0,
td.state.LastBlockHash(), td.state.Genesis().Hash())
msg := message.NewHelloMessage(pid, "unknown-peer", service.New(service.FullNode),
td.RandHeight(), td.RandHash(), td.state.Genesis().Hash())
msg.Sign([]*bls.ValidatorKey{valKey})

from := td.RandPeerID()
Expand All @@ -38,8 +38,8 @@ func TestParsingHelloMessages(t *testing.T) {
invGenHash := td.RandHash()
valKey := td.RandValKey()
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "bad-genesis", 0, 0,
td.state.LastBlockHash(), invGenHash)
msg := message.NewHelloMessage(pid, "bad-genesis", service.New(service.FullNode),
td.RandHeight(), td.RandHash(), invGenHash)
msg.Sign([]*bls.ValidatorKey{valKey})

td.receivingNewMessage(td.sync, msg, pid)
Expand All @@ -51,10 +51,9 @@ func TestParsingHelloMessages(t *testing.T) {
t.Run("Receiving a Hello message from a peer. The time difference is greater than or equal to -10",
func(t *testing.T) {
valKey := td.RandValKey()
height := td.RandUint32NonZero(td.state.LastBlockHeight())
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "kitty", height, service.New(service.Network),
td.state.LastBlockHash(), td.state.Genesis().Hash())
msg := message.NewHelloMessage(pid, "kitty", service.New(service.FullNode),
td.RandHeight(), td.RandHash(), td.state.Genesis().Hash())
msg.Sign([]*bls.ValidatorKey{valKey})

msg.MyTimeUnixMilli = msg.MyTime().Add(-10 * time.Second).UnixMilli()
Expand All @@ -67,10 +66,9 @@ func TestParsingHelloMessages(t *testing.T) {
t.Run("Receiving Hello message from a peer. Difference is less or equal than 20 seconds.",
func(t *testing.T) {
valKey := td.RandValKey()
height := td.RandUint32NonZero(td.state.LastBlockHeight())
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "kitty", height, service.New(service.Network),
td.state.LastBlockHash(), td.state.Genesis().Hash())
msg := message.NewHelloMessage(pid, "kitty", service.New(service.FullNode),
td.RandHeight(), td.RandHash(), td.state.Genesis().Hash())
msg.Sign([]*bls.ValidatorKey{valKey})

msg.MyTimeUnixMilli = msg.MyTime().Add(20 * time.Second).UnixMilli()
Expand All @@ -83,10 +81,9 @@ func TestParsingHelloMessages(t *testing.T) {
t.Run("Non supporting version.",
func(t *testing.T) {
valKey := td.RandValKey()
height := td.RandUint32NonZero(td.state.LastBlockHeight())
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "kitty", height, service.New(service.Network),
td.state.LastBlockHash(), td.state.Genesis().Hash())
msg := message.NewHelloMessage(pid, "kitty", service.New(service.FullNode),
td.RandHeight(), td.RandHash(), td.state.Genesis().Hash())
nodeAgent := version.NodeAgent
nodeAgent.Version = version.Version{
Major: 1,
Expand All @@ -105,10 +102,9 @@ func TestParsingHelloMessages(t *testing.T) {
t.Run("Invalid agent.",
func(t *testing.T) {
valKey := td.RandValKey()
height := td.RandUint32NonZero(td.state.LastBlockHeight())
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "kitty", height, service.New(service.Network),
td.state.LastBlockHash(), td.state.Genesis().Hash())
msg := message.NewHelloMessage(pid, "kitty", service.New(service.FullNode),
td.RandHeight(), td.RandHash(), td.state.Genesis().Hash())
msg.Agent = "invalid-agent"
msg.Sign([]*bls.ValidatorKey{valKey})

Expand All @@ -121,10 +117,10 @@ func TestParsingHelloMessages(t *testing.T) {
t.Run("Receiving Hello message from a peer. It should be acknowledged and updates the peer info",
func(t *testing.T) {
valKey := td.RandValKey()
height := td.RandUint32NonZero(td.state.LastBlockHeight())
pid := td.RandPeerID()
msg := message.NewHelloMessage(pid, "kitty", height, service.New(service.Network),
td.state.LastBlockHash(), td.state.Genesis().Hash())
peerHeight := td.RandHeight()
msg := message.NewHelloMessage(pid, "kitty", service.New(service.FullNode),
peerHeight, td.RandHash(), td.state.Genesis().Hash())
msg.Sign([]*bls.ValidatorKey{valKey})

td.receivingNewMessage(td.sync, msg, pid)
Expand All @@ -141,8 +137,8 @@ func TestParsingHelloMessages(t *testing.T) {
assert.Equal(t, p.Moniker, "kitty")
assert.Contains(t, p.ConsensusKeys, pub)
assert.Equal(t, p.PeerID, pid)
assert.Equal(t, p.Height, height)
assert.True(t, p.HasNetworkService())
assert.Equal(t, p.Height, peerHeight)
assert.True(t, p.IsFullNode())
})
}

Expand All @@ -154,5 +150,5 @@ func TestSendingHelloMessage(t *testing.T) {

bdl := td.shouldPublishMessageWithThisType(t, message.TypeHello)
assert.True(t, util.IsFlagSet(bdl.Flags, bundle.BundleFlagHandshaking))
assert.True(t, util.IsFlagSet(bdl.Message.(*message.HelloMessage).Services, service.New(service.Network)))
assert.True(t, util.IsFlagSet(bdl.Message.(*message.HelloMessage).Services, service.New(service.FullNode)))
}
2 changes: 1 addition & 1 deletion sync/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func MockingSync(ts *testsuite.TestSuite) *MockSync {
"test-peer-1",
version.NodeAgent.String(),
[]*bls.PublicKey{pub1},
service.New(service.Network))
service.New(service.FullNode))
ps.UpdateHeight(pid1, ts.RandHeight(), ts.RandHash())

ps.UpdateInfo(
Expand Down
Loading

0 comments on commit 89ba289

Please sign in to comment.