|
| 1 | +package adapter |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + stdsha256 "crypto/sha256" |
| 7 | + "encoding/hex" |
| 8 | + "fmt" |
| 9 | + |
| 10 | + tmcryptoed25519 "github.com/cometbft/cometbft/crypto/ed25519" |
| 11 | + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" |
| 12 | + cmttypes "github.com/cometbft/cometbft/types" |
| 13 | + "github.com/libp2p/go-libp2p/core/crypto" |
| 14 | + |
| 15 | + evtypes "github.com/evstack/ev-node/types" |
| 16 | +) |
| 17 | + |
| 18 | +func AggregatorNodeSignatureBytesProvider(adapter *Adapter) evtypes.AggregatorNodeSignatureBytesProvider { |
| 19 | + return func(header *evtypes.Header) ([]byte, error) { |
| 20 | + blockID, err := adapter.Store.GetBlockID(context.Background(), header.Height()) |
| 21 | + if err != nil && header.Height() > 1 { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + |
| 25 | + return createVote(header, blockID), nil |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func SyncNodeSignatureBytesProvider(adapter *Adapter) evtypes.SyncNodeSignatureBytesProvider { |
| 30 | + return func(ctx context.Context, header *evtypes.Header, data *evtypes.Data) ([]byte, error) { |
| 31 | + blockHeight := header.Height() |
| 32 | + blockID := &cmttypes.BlockID{} |
| 33 | + |
| 34 | + if header.Height() > 1 { // first block has an empty block ID |
| 35 | + cmtTxs := make(cmttypes.Txs, len(data.Txs)) |
| 36 | + for i := range data.Txs { |
| 37 | + cmtTxs[i] = cmttypes.Tx(data.Txs[i]) |
| 38 | + } |
| 39 | + |
| 40 | + lastCommit, err := adapter.GetLastCommit(ctx, blockHeight) |
| 41 | + if err != nil { |
| 42 | + return nil, fmt.Errorf("get last commit: %w", err) |
| 43 | + } |
| 44 | + |
| 45 | + abciHeader, err := ToABCIHeader(*header, lastCommit) |
| 46 | + if err != nil { |
| 47 | + return nil, fmt.Errorf("compute header hash: %w", err) |
| 48 | + } |
| 49 | + |
| 50 | + currentState, err := adapter.Store.LoadState(ctx) |
| 51 | + if err != nil { |
| 52 | + return nil, fmt.Errorf("load state: %w", err) |
| 53 | + } |
| 54 | + |
| 55 | + _, blockID, err = MakeABCIBlock(blockHeight, cmtTxs, currentState, abciHeader, lastCommit) |
| 56 | + if err != nil { |
| 57 | + return nil, fmt.Errorf("make ABCI block: %w", err) |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return createVote(header, blockID), nil |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +// createVote builds the vote for the given header and block ID to be signed. |
| 66 | +func createVote(header *evtypes.Header, blockID *cmttypes.BlockID) []byte { |
| 67 | + vote := cmtproto.Vote{ |
| 68 | + Type: cmtproto.PrecommitType, |
| 69 | + Height: int64(header.Height()), //nolint:gosec |
| 70 | + BlockID: blockID.ToProto(), |
| 71 | + Round: 0, |
| 72 | + Timestamp: header.Time(), |
| 73 | + ValidatorAddress: header.ProposerAddress, |
| 74 | + ValidatorIndex: 0, |
| 75 | + } |
| 76 | + |
| 77 | + chainID := header.ChainID() |
| 78 | + consensusVoteBytes := cmttypes.VoteSignBytes(chainID, &vote) |
| 79 | + |
| 80 | + return consensusVoteBytes |
| 81 | +} |
| 82 | + |
| 83 | +// ValidatorHasher returns a function that calculates the ValidatorHash |
| 84 | +// compatible with CometBFT. This function is intended to be injected into ev-node's Manager. |
| 85 | +func ValidatorHasherProvider() func(proposerAddress []byte, pubKey crypto.PubKey) (evtypes.Hash, error) { |
| 86 | + return func(proposerAddress []byte, pubKey crypto.PubKey) (evtypes.Hash, error) { |
| 87 | + var calculatedHash evtypes.Hash |
| 88 | + |
| 89 | + var cometBftPubKey tmcryptoed25519.PubKey |
| 90 | + if pubKey.Type() == crypto.Ed25519 { |
| 91 | + rawKey, err := pubKey.Raw() |
| 92 | + if err != nil { |
| 93 | + return calculatedHash, fmt.Errorf("failed to get raw bytes from libp2p public key: %w", err) |
| 94 | + } |
| 95 | + if len(rawKey) != tmcryptoed25519.PubKeySize { |
| 96 | + return calculatedHash, fmt.Errorf("libp2p public key size (%d) does not match CometBFT Ed25519 PubKeySize (%d)", len(rawKey), tmcryptoed25519.PubKeySize) |
| 97 | + } |
| 98 | + cometBftPubKey = rawKey |
| 99 | + } else { |
| 100 | + return calculatedHash, fmt.Errorf("unsupported public key type '%s', expected Ed25519 for CometBFT compatibility", pubKey.Type()) |
| 101 | + } |
| 102 | + |
| 103 | + votingPower := int64(1) |
| 104 | + sequencerValidator := cmttypes.NewValidator(cometBftPubKey, votingPower) |
| 105 | + |
| 106 | + derivedAddress := sequencerValidator.Address.Bytes() |
| 107 | + if !bytes.Equal(derivedAddress, proposerAddress) { |
| 108 | + return calculatedHash, fmt.Errorf("CRITICAL MISMATCH - derived validator address (%s) does not match expected proposer address (%s). PubKey used for derivation: %s", |
| 109 | + hex.EncodeToString(derivedAddress), |
| 110 | + hex.EncodeToString(proposerAddress), |
| 111 | + hex.EncodeToString(cometBftPubKey.Bytes())) |
| 112 | + } |
| 113 | + |
| 114 | + sequencerValidatorSet := cmttypes.NewValidatorSet([]*cmttypes.Validator{sequencerValidator}) |
| 115 | + |
| 116 | + hashSumBytes := sequencerValidatorSet.Hash() |
| 117 | + |
| 118 | + calculatedHash = make(evtypes.Hash, stdsha256.Size) |
| 119 | + copy(calculatedHash, hashSumBytes) |
| 120 | + |
| 121 | + return calculatedHash, nil |
| 122 | + } |
| 123 | +} |
0 commit comments