|
| 1 | +package consensus |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/NethermindEth/juno/blockchain" |
| 10 | + "github.com/NethermindEth/juno/builder" |
| 11 | + consensusDB "github.com/NethermindEth/juno/consensus/db" |
| 12 | + "github.com/NethermindEth/juno/consensus/driver" |
| 13 | + "github.com/NethermindEth/juno/consensus/p2p" |
| 14 | + "github.com/NethermindEth/juno/consensus/p2p/config" |
| 15 | + "github.com/NethermindEth/juno/consensus/proposal" |
| 16 | + "github.com/NethermindEth/juno/consensus/proposer" |
| 17 | + "github.com/NethermindEth/juno/consensus/starknet" |
| 18 | + "github.com/NethermindEth/juno/consensus/tendermint" |
| 19 | + "github.com/NethermindEth/juno/consensus/types" |
| 20 | + "github.com/NethermindEth/juno/consensus/votecounter" |
| 21 | + "github.com/NethermindEth/juno/core/felt" |
| 22 | + "github.com/NethermindEth/juno/db" |
| 23 | + "github.com/NethermindEth/juno/utils" |
| 24 | + "github.com/NethermindEth/juno/vm" |
| 25 | + "github.com/libp2p/go-libp2p" |
| 26 | + "github.com/libp2p/go-libp2p/core/crypto" |
| 27 | + "github.com/libp2p/go-libp2p/core/host" |
| 28 | + "github.com/libp2p/go-libp2p/core/peer" |
| 29 | + "github.com/sourcegraph/conc/pool" |
| 30 | +) |
| 31 | + |
| 32 | +type ConsensusServices struct { |
| 33 | + Host host.Host |
| 34 | + Proposer proposer.Proposer[starknet.Value, starknet.Hash] |
| 35 | + P2P p2p.P2P[starknet.Value, starknet.Hash, starknet.Address] |
| 36 | + Driver *driver.Driver[starknet.Value, starknet.Hash, starknet.Address] |
| 37 | + CommitListener driver.CommitListener[starknet.Value, starknet.Hash, starknet.Address] |
| 38 | +} |
| 39 | + |
| 40 | +func Init( |
| 41 | + logger *utils.ZapLogger, |
| 42 | + database db.KeyValueStore, |
| 43 | + blockchain *blockchain.Blockchain, |
| 44 | + vm vm.VM, |
| 45 | + nodeAddress *starknet.Address, |
| 46 | + validators votecounter.Validators[starknet.Address], |
| 47 | + timeoutFn driver.TimeoutFn, |
| 48 | + hostAddress string, |
| 49 | + hostPrivateKey crypto.PrivKey, |
| 50 | +) (ConsensusServices, error) { |
| 51 | + chainHeight, err := blockchain.Height() |
| 52 | + if err != nil && !errors.Is(err, db.ErrKeyNotFound) { |
| 53 | + return ConsensusServices{}, err |
| 54 | + } |
| 55 | + currentHeight := types.Height(chainHeight + 1) |
| 56 | + |
| 57 | + tendermintDB := consensusDB.NewTendermintDB[starknet.Value, starknet.Hash, starknet.Address](database, currentHeight) |
| 58 | + |
| 59 | + executor := builder.NewExecutor(blockchain, vm, logger, false, true) // TODO: We're currently skipping signature validation |
| 60 | + builder := builder.New(blockchain, executor) |
| 61 | + |
| 62 | + proposalStore := proposal.ProposalStore[starknet.Hash]{} |
| 63 | + proposer := proposer.New(logger, &builder, &proposalStore, *nodeAddress, toValue) |
| 64 | + stateMachine := tendermint.New(tendermintDB, logger, *nodeAddress, proposer, validators, currentHeight) |
| 65 | + |
| 66 | + host, err := libp2p.New( |
| 67 | + libp2p.ListenAddrStrings(hostAddress), |
| 68 | + libp2p.Identity(hostPrivateKey), |
| 69 | + // libp2p.UserAgent(makeAgentName(version)), |
| 70 | + // // Use address factory to add the public address to the list of |
| 71 | + // // addresses that the node will advertise. |
| 72 | + // libp2p.AddrsFactory(addressFactory), |
| 73 | + // If we know the public ip, enable the relay service. |
| 74 | + libp2p.EnableRelayService(), |
| 75 | + // When listening behind NAT, enable peers to try to poke thought the |
| 76 | + // NAT in order to reach the node. |
| 77 | + libp2p.EnableHolePunching(), |
| 78 | + // Try to open a port in the NAT router to accept incoming connections. |
| 79 | + libp2p.NATPortMap(), |
| 80 | + ) |
| 81 | + if err != nil { |
| 82 | + return ConsensusServices{}, err |
| 83 | + } |
| 84 | + |
| 85 | + p2p := p2p.New(host, logger, &builder, &proposalStore, currentHeight, &config.DefaultBufferSizes) |
| 86 | + |
| 87 | + commitListener := driver.NewCommitListener(logger, &proposalStore, proposer, p2p) |
| 88 | + driver := driver.New(logger, tendermintDB, stateMachine, commitListener, p2p, timeoutFn) |
| 89 | + |
| 90 | + return ConsensusServices{ |
| 91 | + Host: host, |
| 92 | + Proposer: proposer, |
| 93 | + P2P: p2p, |
| 94 | + Driver: &driver, |
| 95 | + CommitListener: commitListener, |
| 96 | + }, nil |
| 97 | +} |
| 98 | + |
| 99 | +func Connect(ctx context.Context, host host.Host, peers string) error { |
| 100 | + if peers == "" { |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + pool := pool.New().WithErrors().WithFirstError() |
| 105 | + |
| 106 | + for peerStr := range strings.SplitSeq(peers, ",") { |
| 107 | + pool.Go(func() error { |
| 108 | + peerAddr, err := peer.AddrInfoFromString(peerStr) |
| 109 | + if err != nil { |
| 110 | + return fmt.Errorf("unable to parse peer address %q: %w", peerStr, err) |
| 111 | + } |
| 112 | + |
| 113 | + if err := host.Connect(ctx, *peerAddr); err != nil { |
| 114 | + return fmt.Errorf("unable to connect to %q: %w", peerStr, err) |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + return pool.Wait() |
| 122 | +} |
| 123 | + |
| 124 | +func toValue(value *felt.Felt) starknet.Value { |
| 125 | + return starknet.Value(*value) |
| 126 | +} |
0 commit comments