Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node/Gov: Add target to db #3791

Merged
merged 3 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 53 additions & 16 deletions node/pkg/db/governor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type Transfer struct {
EmitterAddress vaa.Address
MsgID string
Hash string
TargetAddress vaa.Address
TargetChain vaa.ChainID
}

func (t *Transfer) Marshal() ([]byte, error) {
Expand All @@ -72,6 +74,8 @@ func (t *Transfer) Marshal() ([]byte, error) {
if len(t.Hash) > 0 {
buf.Write([]byte(t.Hash))
}
vaa.MustWrite(buf, binary.BigEndian, t.TargetChain)
buf.Write(t.TargetAddress[:])
return buf.Bytes(), nil
}

Expand All @@ -91,17 +95,17 @@ func UnmarshalTransfer(data []byte) (*Transfer, error) {
}

if err := binary.Read(reader, binary.BigEndian, &t.OriginChain); err != nil {
return nil, fmt.Errorf("failed to read token chain id: %w", err)
return nil, fmt.Errorf("failed to read origin chain id: %w", err)
}

originAddress := vaa.Address{}
if n, err := reader.Read(originAddress[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read emitter address [%d]: %w", n, err)
return nil, fmt.Errorf("failed to read origin address [%d]: %w", n, err)
}
t.OriginAddress = originAddress

if err := binary.Read(reader, binary.BigEndian, &t.EmitterChain); err != nil {
return nil, fmt.Errorf("failed to read token chain id: %w", err)
return nil, fmt.Errorf("failed to read emitter chain id: %w", err)
}

emitterAddress := vaa.Address{}
Expand All @@ -118,8 +122,8 @@ func UnmarshalTransfer(data []byte) (*Transfer, error) {
if msgIdLen > 0 {
msgID := make([]byte, msgIdLen)
n, err := reader.Read(msgID)
if err != nil || n == 0 {
return nil, fmt.Errorf("failed to read vaa id [%d]: %w", n, err)
if err != nil || n != int(msgIdLen) {
return nil, fmt.Errorf("failed to read msg id [%d]: %w", n, err)
}
t.MsgID = string(msgID[:n])
}
Expand All @@ -132,12 +136,22 @@ func UnmarshalTransfer(data []byte) (*Transfer, error) {
if hashLen > 0 {
hash := make([]byte, hashLen)
n, err := reader.Read(hash)
if err != nil || n == 0 {
if err != nil || n != int(hashLen) {
return nil, fmt.Errorf("failed to read hash [%d]: %w", n, err)
}
t.Hash = string(hash[:n])
}

if err := binary.Read(reader, binary.BigEndian, &t.TargetChain); err != nil {
return nil, fmt.Errorf("failed to read target chain id: %w", err)
}

targetAddress := vaa.Address{}
if n, err := reader.Read(targetAddress[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read target address [%d]: %w", n, err)
}
t.TargetAddress = targetAddress

return t, nil
}

Expand All @@ -157,17 +171,17 @@ func unmarshalOldTransfer(data []byte) (*Transfer, error) {
}

if err := binary.Read(reader, binary.BigEndian, &t.OriginChain); err != nil {
return nil, fmt.Errorf("failed to read token chain id: %w", err)
return nil, fmt.Errorf("failed to read origin chain id: %w", err)
}

originAddress := vaa.Address{}
if n, err := reader.Read(originAddress[:]); err != nil || n != 32 {
return nil, fmt.Errorf("failed to read emitter address [%d]: %w", n, err)
return nil, fmt.Errorf("failed to read origin address [%d]: %w", n, err)
}
t.OriginAddress = originAddress

if err := binary.Read(reader, binary.BigEndian, &t.EmitterChain); err != nil {
return nil, fmt.Errorf("failed to read token chain id: %w", err)
return nil, fmt.Errorf("failed to read emitter chain id: %w", err)
}

emitterAddress := vaa.Address{}
Expand All @@ -176,12 +190,35 @@ func unmarshalOldTransfer(data []byte) (*Transfer, error) {
}
t.EmitterAddress = emitterAddress

msgID := make([]byte, 256)
n, err := reader.Read(msgID)
if err != nil || n == 0 {
return nil, fmt.Errorf("failed to read vaa id [%d]: %w", n, err)
msgIdLen := uint16(0)
if err := binary.Read(reader, binary.BigEndian, &msgIdLen); err != nil {
return nil, fmt.Errorf("failed to read msgID length: %w", err)
}
t.MsgID = string(msgID[:n])

if msgIdLen > 0 {
msgID := make([]byte, msgIdLen)
n, err := reader.Read(msgID)
if err != nil || n == 0 {
return nil, fmt.Errorf("failed to read vaa id [%d]: %w", n, err)
}
t.MsgID = string(msgID[:n])
}

hashLen := uint16(0)
if err := binary.Read(reader, binary.BigEndian, &hashLen); err != nil {
return nil, fmt.Errorf("failed to read hash length: %w", err)
}

if hashLen > 0 {
hash := make([]byte, hashLen)
n, err := reader.Read(hash)
if err != nil || n == 0 {
return nil, fmt.Errorf("failed to read hash [%d]: %w", n, err)
}
t.Hash = string(hash[:n])
}

// Do not include the target chain or address.

return t, nil
}
Expand Down Expand Up @@ -238,10 +275,10 @@ func UnmarshalPendingTransfer(data []byte, isOld bool) (*PendingTransfer, error)
return p, nil
}

const oldTransfer = "GOV:XFER:"
const oldTransfer = "GOV:XFER2:"
const oldTransferLen = len(oldTransfer)

const transfer = "GOV:XFER2:"
const transfer = "GOV:XFER3:"
const transferLen = len(transfer)

// Since we are changing the DB format of pending entries, we will use a new tag in the pending key field.
Expand Down
Loading
Loading