Skip to content

Commit

Permalink
Node: Change TxHash to TxID in MessagePublication
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-riley committed Jan 14, 2025
1 parent f1580c3 commit a3c9247
Show file tree
Hide file tree
Showing 34 changed files with 597 additions and 281 deletions.
14 changes: 7 additions & 7 deletions node/hack/accountant/send_obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func testSubmit(
Payload, _ := hex.DecodeString("010000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000002d8be6bf0baa74e0a907016679cae9190e80dd0a0002000000000000000000000000c10820983f33456ce7beb3a046f5a83fa34f027d0c200000000000000000000000000000000000000000000000000000000000000000")

msg := common.MessagePublication{
TxHash: TxHash,
TxID: TxHash.Bytes(),
Timestamp: timestamp,
Nonce: uint32(0),
Sequence: sequence,
Expand Down Expand Up @@ -167,7 +167,7 @@ func testBatch(
msgs := []*common.MessagePublication{}

msg1 := common.MessagePublication{
TxHash: TxHash,
TxID: TxHash.Bytes(),
Timestamp: timestamp,
Nonce: nonce,
Sequence: sequence,
Expand All @@ -181,7 +181,7 @@ func testBatch(
nonce = nonce + 1
sequence = sequence + 1
msg2 := common.MessagePublication{
TxHash: TxHash,
TxID: TxHash.Bytes(),
Timestamp: time.Now(),
Nonce: nonce,
Sequence: sequence,
Expand Down Expand Up @@ -246,7 +246,7 @@ func testBatchWithcommitted(

logger.Info("submitting a single transfer that should work")
msg1 := common.MessagePublication{
TxHash: TxHash,
TxID: TxHash.Bytes(),
Timestamp: timestamp,
Nonce: nonce,
Sequence: sequence,
Expand All @@ -269,7 +269,7 @@ func testBatchWithcommitted(
nonce = nonce + 1
sequence = sequence + 1
msg2 := common.MessagePublication{
TxHash: TxHash,
TxID: TxHash.Bytes(),
Timestamp: time.Now(),
Nonce: nonce,
Sequence: sequence,
Expand Down Expand Up @@ -338,7 +338,7 @@ func testBatchWithDigestError(

logger.Info("submitting a single transfer that should work")
msg1 := common.MessagePublication{
TxHash: TxHash,
TxID: TxHash.Bytes(),
Timestamp: timestamp,
Nonce: nonce,
Sequence: sequence,
Expand All @@ -361,7 +361,7 @@ func testBatchWithDigestError(
nonce = nonce + 1
sequence = sequence + 1
msg2 := common.MessagePublication{
TxHash: TxHash,
TxID: TxHash.Bytes(),
Timestamp: time.Now(),
Nonce: nonce,
Sequence: sequence,
Expand Down
2 changes: 1 addition & 1 deletion node/hack/repair_terra/repair.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func EventsToMessagePublications(contract string, txHash string, events []gjson.
continue
}
messagePublication := &common.MessagePublication{
TxHash: txHashValue,
TxID: txHashValue.Bytes(),
Timestamp: time.Unix(blockTimeInt, 0),
Nonce: uint32(nonceInt),
Sequence: sequenceInt,
Expand Down
16 changes: 8 additions & 8 deletions node/pkg/accountant/accountant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ func newAccountantForTest(
}

// Converts a string into a go-ethereum Hash object used as test input.
func hashFromString(str string) ethCommon.Hash { //nolint:unparam
func hashToTxID(str string) []byte {
if (len(str) > 2) && (str[0] == '0') && (str[1] == 'x') {
str = str[2:]
}

return ethCommon.HexToHash(str)
return ethCommon.HexToHash(str).Bytes()
}

// Note this method assumes 18 decimals for the amount.
Expand Down Expand Up @@ -188,7 +188,7 @@ func TestVaaFromUninterestingEmitter(t *testing.T) {
var payload = []byte{1, 97, 97, 97, 97, 97}

msg := common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4064"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(1),
Sequence: uint64(1),
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestVaaForUninterestingPayloadType(t *testing.T) {
var payload = []byte{2, 97, 97, 97, 97, 97}

msg := common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(1),
Sequence: uint64(1),
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestInterestingTransferShouldNotBeBlockedWhenNotEnforcingAccountant(t *test
)

msg := common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(1),
Sequence: uint64(1),
Expand Down Expand Up @@ -295,7 +295,7 @@ func TestInterestingTransferShouldBeBlockedWhenEnforcingAccountant(t *testing.T)
)

msg := common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(1),
Sequence: uint64(1),
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestForDeadlock(t *testing.T) {
)

msg := common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(1),
Sequence: uint64(1683136244),
Expand All @@ -374,7 +374,7 @@ func TestForDeadlock(t *testing.T) {
assert.Equal(t, 1, len(acct.msgChan))

msg2 := common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(1),
Sequence: uint64(1683136244),
Expand Down
2 changes: 1 addition & 1 deletion node/pkg/accountant/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (mo *MissingObservation) makeAuditKey() string {

// makeAuditKey creates an audit map key from a pending observation entry.
func (pe *pendingEntry) makeAuditKey() string {
return fmt.Sprintf("%d-%s", pe.msg.EmitterChain, strings.TrimPrefix(pe.msg.TxHash.String(), "0x"))
return fmt.Sprintf("%d-%s", pe.msg.EmitterChain, strings.TrimPrefix(pe.msg.TxIDString(), "0x"))
}

// audit is the runnable that executes the audit each interval.
Expand Down
10 changes: 5 additions & 5 deletions node/pkg/accountant/ntt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestNttParseMsgSuccess(t *testing.T) {
}

msg := &common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(42),
Sequence: uint64(123456),
Expand All @@ -77,7 +77,7 @@ func TestNttParseMsgWrongEmitterChain(t *testing.T) {
}

msg := &common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(42),
Sequence: uint64(123456),
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestNttParseMsgWrongEmitterAddress(t *testing.T) {
}

msg := &common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1654543099), 0),
Nonce: uint32(42),
Sequence: uint64(123456),
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestNttParseArMsgSuccess(t *testing.T) {
require.NoError(t, err)

msg := &common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1708575745), 0),
Nonce: uint32(0),
Sequence: uint64(259),
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestNttParseArMsgUnknownArEmitter(t *testing.T) {
require.NoError(t, err)

msg := &common.MessagePublication{
TxHash: hashFromString("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
TxID: hashToTxID("0x06f541f5ecfc43407c31587aa6ac3a689e8960f36dc23c332db5510dfc6a4063"),
Timestamp: time.Unix(int64(1708575745), 0),
Nonce: uint32(0),
Sequence: uint64(259),
Expand Down
4 changes: 2 additions & 2 deletions node/pkg/accountant/submit_obs.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func SubmitObservationsToContract(
obs := make([]Observation, len(msgs))
for idx, msg := range msgs {
obs[idx] = Observation{
TxHash: msg.TxHash.Bytes(),
TxHash: msg.TxID,
Timestamp: uint32(msg.Timestamp.Unix()),
Nonce: msg.Nonce,
EmitterChain: uint16(msg.EmitterChain),
Expand All @@ -321,7 +321,7 @@ func SubmitObservationsToContract(
logger.Debug("in SubmitObservationsToContract, encoding observation",
zap.String("contract", contract),
zap.Int("idx", idx),
zap.String("txHash", msg.TxHash.String()), zap.String("encTxHash", hex.EncodeToString(obs[idx].TxHash[:])),
zap.String("txHash", msg.TxIDString()), zap.String("encTxHash", hex.EncodeToString(obs[idx].TxHash[:])),
zap.Stringer("timeStamp", msg.Timestamp), zap.Uint32("encTimestamp", obs[idx].Timestamp),
zap.Uint32("nonce", msg.Nonce), zap.Uint32("encNonce", obs[idx].Nonce),
zap.Stringer("emitterChain", msg.EmitterChain), zap.Uint16("encEmitterChain", obs[idx].EmitterChain),
Expand Down
4 changes: 1 addition & 3 deletions node/pkg/accountant/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/certusone/wormhole/node/pkg/common"
"github.com/wormhole-foundation/wormhole/sdk/vaa"

ethCommon "github.com/ethereum/go-ethereum/common"

tmAbci "github.com/tendermint/tendermint/abci/types"
tmHttp "github.com/tendermint/tendermint/rpc/client/http"
tmCoreTypes "github.com/tendermint/tendermint/rpc/core/types"
Expand Down Expand Up @@ -181,7 +179,7 @@ func (acct *Accountant) processPendingTransfer(xfer *WasmObservation, tag string
)

msg := &common.MessagePublication{
TxHash: ethCommon.BytesToHash(xfer.TxHash),
TxID: xfer.TxHash,
Timestamp: time.Unix(int64(xfer.Timestamp), 0),
Nonce: xfer.Nonce,
Sequence: xfer.Sequence,
Expand Down
2 changes: 1 addition & 1 deletion node/pkg/adminrpc/adminserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ func (s *nodePrivilegedService) InjectGovernanceVAA(ctx context.Context, req *no
vaaInjectionsTotal.Inc()

s.injectC <- &common.MessagePublication{
TxHash: ethcommon.Hash{},
TxID: ethcommon.Hash{}.Bytes(),
Timestamp: v.Timestamp,
Nonce: v.Nonce,
Sequence: v.Sequence,
Expand Down
48 changes: 31 additions & 17 deletions node/pkg/common/chainlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
"go.uber.org/zap"

"github.com/ethereum/go-ethereum/common"
)

const HashLength = 32
const AddressLength = 32

type MessagePublication struct {
TxHash common.Hash // TODO: rename to identifier? on Solana, this isn't actually the tx hash
TxID []byte
Timestamp time.Time

Nonce uint32
Expand All @@ -35,6 +34,10 @@ type MessagePublication struct {
Unreliable bool
}

func (msg *MessagePublication) TxIDString() string {
return "0x" + hex.EncodeToString(msg.TxID)
}

func (msg *MessagePublication) MessageID() []byte {
return []byte(msg.MessageIDString())
}
Expand All @@ -48,7 +51,12 @@ const minMsgLength = 88 // Marshalled length with empty payload
func (msg *MessagePublication) Marshal() ([]byte, error) {
buf := new(bytes.Buffer)

buf.Write(msg.TxHash[:])
if len(msg.TxID) > math.MaxUint32 {
return nil, fmt.Errorf("TxID too long")
}
vaa.MustWrite(buf, binary.BigEndian, uint32(len(msg.TxID)))
buf.Write(msg.TxID)

vaa.MustWrite(buf, binary.BigEndian, uint32(msg.Timestamp.Unix()))
vaa.MustWrite(buf, binary.BigEndian, msg.Nonce)
vaa.MustWrite(buf, binary.BigEndian, msg.Sequence)
Expand All @@ -61,13 +69,11 @@ func (msg *MessagePublication) Marshal() ([]byte, error) {
return buf.Bytes(), nil
}

const oldMinMsgLength = 83 // Old marshalled length with empty payload

// UnmarshalOldMessagePublicationBeforeIsReobservation deserializes a MessagePublication from prior to the addition of IsReobservation.
// UnmarshalOldMessagePublicationWithTxHash deserializes a MessagePublication from when the TxHash was a fixed size ethCommon.Hash.
// This function can be deleted once all guardians have been upgraded. That's why the code is just duplicated.
func UnmarshalOldMessagePublicationBeforeIsReobservation(data []byte) (*MessagePublication, error) {
if len(data) < oldMinMsgLength {
return nil, errors.New("message is too short")
func UnmarshalOldMessagePublicationWithTxHash(data []byte) (*MessagePublication, error) {
if len(data) < minMsgLength {
return nil, fmt.Errorf("message is too short")
}

msg := &MessagePublication{}
Expand All @@ -78,7 +84,7 @@ func UnmarshalOldMessagePublicationBeforeIsReobservation(data []byte) (*MessageP
if n, err := reader.Read(txHash[:]); err != nil || n != HashLength {
return nil, fmt.Errorf("failed to read TxHash [%d]: %w", n, err)
}
msg.TxHash = txHash
msg.TxID = txHash.Bytes()

unixSeconds := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &unixSeconds); err != nil {
Expand Down Expand Up @@ -108,6 +114,10 @@ func UnmarshalOldMessagePublicationBeforeIsReobservation(data []byte) (*MessageP
}
msg.EmitterAddress = emitterAddress

if err := binary.Read(reader, binary.BigEndian, &msg.IsReobservation); err != nil {
return nil, fmt.Errorf("failed to read isReobservation: %w", err)
}

payload := make([]byte, reader.Len())
n, err := reader.Read(payload)
if err != nil || n == 0 {
Expand All @@ -128,11 +138,15 @@ func UnmarshalMessagePublication(data []byte) (*MessagePublication, error) {

reader := bytes.NewReader(data[:])

txHash := common.Hash{}
if n, err := reader.Read(txHash[:]); err != nil || n != HashLength {
return nil, fmt.Errorf("failed to read TxHash [%d]: %w", n, err)
txIdLen := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &txIdLen); err != nil {
return nil, fmt.Errorf("failed to read TxID len: %w", err)
}

msg.TxID = make([]byte, txIdLen)
if n, err := reader.Read(msg.TxID[:]); err != nil || n != int(txIdLen) {
return nil, fmt.Errorf("failed to read TxID [%d]: %w", n, err)
}
msg.TxHash = txHash

unixSeconds := uint32(0)
if err := binary.Read(reader, binary.BigEndian, &unixSeconds); err != nil {
Expand Down Expand Up @@ -229,7 +243,7 @@ func (msg *MessagePublication) CreateDigest() string {
// TODO refactor the codebase to use this function instead of manually logging the message with inconsistent fields
func (msg *MessagePublication) ZapFields(fields ...zap.Field) []zap.Field {
return append(fields,
zap.Stringer("tx", msg.TxHash),
zap.String("tx", msg.TxIDString()),
zap.Time("timestamp", msg.Timestamp),
zap.Uint32("nonce", msg.Nonce),
zap.Uint8("consistency", msg.ConsistencyLevel),
Expand Down
Loading

0 comments on commit a3c9247

Please sign in to comment.