Browse Source

fix: moved message fee from int64 to string and uint256 (#4399)

* fix: moved message fee from int64 to string and uint256

* negative check
Fernando Garcia 5 months ago
parent
commit
b5157ec2ad

+ 2 - 2
node/cmd/guardiand/admintemplate.go

@@ -78,7 +78,7 @@ var governanceTargetChain *string
 var governanceCallData *string
 
 var coreBridgeSetMessageFeeChainId *string
-var coreBridgeSetMessageFeeMessageFee *uint64
+var coreBridgeSetMessageFeeMessageFee *string
 
 func init() {
 	governanceFlagSet := pflag.NewFlagSet("governance", pflag.ExitOnError)
@@ -210,7 +210,7 @@ func init() {
 	// flags for the core-bridge-set-message-fee command
 	coreBridgeSetMessageFeeFlagSet := pflag.NewFlagSet("core-bridge-set-message-fee", pflag.ExitOnError)
 	coreBridgeSetMessageFeeChainId = coreBridgeSetMessageFeeFlagSet.String("chain-id", "", "Chain ID")
-	coreBridgeSetMessageFeeMessageFee = coreBridgeSetMessageFeeFlagSet.Uint64("message-fee", 0, "New message fee")
+	coreBridgeSetMessageFeeMessageFee = coreBridgeSetMessageFeeFlagSet.String("message-fee", "", "New message fee")
 	AdminClientCoreBridgeSetMessageFeeCmd.Flags().AddFlagSet(coreBridgeSetMessageFeeFlagSet)
 	TemplateCmd.AddCommand(AdminClientCoreBridgeSetMessageFeeCmd)
 

+ 14 - 1
node/pkg/adminrpc/adminserver.go

@@ -669,9 +669,22 @@ func coreBridgeSetMessageFeeToVaa(req *nodev1.CoreBridgeSetMessageFee, timestamp
 		return nil, fmt.Errorf("failed to convert chain id: %w", err)
 	}
 
+	new_fee_big := big.NewInt(0)
+	new_fee_big, ok := new_fee_big.SetString(req.MessageFee, 10)
+	if !ok {
+		return nil, errors.New("invalid new fee")
+	}
+	new_fee, overflow := uint256.FromBig(new_fee_big)
+	if overflow {
+		return nil, errors.New("new fee overflow")
+	}
+	if new_fee_big.Cmp(big.NewInt(0)) < 0 {
+		return nil, errors.New("new fee cannot be negative")
+	}
+
 	body, err := vaa.BodyCoreBridgeSetMessageFee{
 		ChainID:    chainId,
-		MessageFee: uint64(req.MessageFee),
+		MessageFee: new_fee,
 	}.Serialize()
 	if err != nil {
 		return nil, fmt.Errorf("failed to serialize governance body: %w", err)

+ 4 - 4
node/pkg/proto/node/v1/node.pb.go

@@ -3194,7 +3194,7 @@ type CoreBridgeSetMessageFee struct {
 	// ID of the chain of the core bridge contract where the message fee should be updated (uint16).
 	ChainId uint32 `protobuf:"varint,1,opt,name=chain_id,json=chainId,proto3" json:"chain_id,omitempty"`
 	// New message fee.
-	MessageFee uint64 `protobuf:"varint,2,opt,name=message_fee,json=messageFee,proto3" json:"message_fee,omitempty"`
+	MessageFee string `protobuf:"bytes,2,opt,name=message_fee,json=messageFee,proto3" json:"message_fee,omitempty"`
 }
 
 func (x *CoreBridgeSetMessageFee) Reset() {
@@ -3236,11 +3236,11 @@ func (x *CoreBridgeSetMessageFee) GetChainId() uint32 {
 	return 0
 }
 
-func (x *CoreBridgeSetMessageFee) GetMessageFee() uint64 {
+func (x *CoreBridgeSetMessageFee) GetMessageFee() string {
 	if x != nil {
 		return x.MessageFee
 	}
-	return 0
+	return ""
 }
 
 // List of guardian set members.
@@ -3755,7 +3755,7 @@ var file_node_v1_node_proto_rawDesc = []byte{
 	0x65, 0x53, 0x65, 0x74, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x46, 0x65, 0x65, 0x12, 0x19,
 	0x0a, 0x08, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d,
 	0x52, 0x07, 0x63, 0x68, 0x61, 0x69, 0x6e, 0x49, 0x64, 0x12, 0x1f, 0x0a, 0x0b, 0x6d, 0x65, 0x73,
-	0x73, 0x61, 0x67, 0x65, 0x5f, 0x66, 0x65, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x04, 0x52, 0x0a,
+	0x73, 0x61, 0x67, 0x65, 0x5f, 0x66, 0x65, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0a,
 	0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x46, 0x65, 0x65, 0x2a, 0x70, 0x0a, 0x10, 0x4d, 0x6f,
 	0x64, 0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x4b, 0x69, 0x6e, 0x64, 0x12, 0x21,
 	0x0a, 0x1d, 0x4d, 0x4f, 0x44, 0x49, 0x46, 0x49, 0x43, 0x41, 0x54, 0x49, 0x4f, 0x4e, 0x5f, 0x4b,

+ 1 - 1
proto/node/v1/node.proto

@@ -470,5 +470,5 @@ message CoreBridgeSetMessageFee {
   // ID of the chain of the core bridge contract where the message fee should be updated (uint16).
   uint32 chain_id = 1;
   // New message fee.
-  uint64 message_fee = 2;
+  string message_fee = 2;
 }

+ 8 - 2
sdk/vaa/payloads.go

@@ -279,7 +279,7 @@ type (
 	// BodyCoreBridgeSetMessageFee is a governance message to set the message fee for the core bridge.
 	BodyCoreBridgeSetMessageFee struct {
 		ChainID    ChainID
-		MessageFee uint64
+		MessageFee *uint256.Int
 	}
 )
 
@@ -503,7 +503,13 @@ func (r BodyWormholeRelayerSetDefaultDeliveryProvider) Serialize() ([]byte, erro
 
 func (r BodyCoreBridgeSetMessageFee) Serialize() ([]byte, error) {
 	payload := &bytes.Buffer{}
-	MustWrite(payload, binary.BigEndian, r.MessageFee)
+	feeBytes := r.MessageFee.Bytes()
+	if len(feeBytes) > 32 {
+		return nil, fmt.Errorf("message fee too large")
+	}
+	padded := make([]byte, 32)
+	copy(padded[32-len(feeBytes):], feeBytes)
+	payload.Write(padded)
 	return serializeBridgeGovernanceVaa(CoreModuleStr, ActionCoreSetMessageFee, r.ChainID, payload.Bytes())
 }
 

+ 2 - 2
sdk/vaa/payloads_test.go

@@ -457,10 +457,10 @@ func TestBodyRecoverChainIdModuleTooLong(t *testing.T) {
 }
 
 func TestBodyCoreBridgeSetMessageFeeSerialize(t *testing.T) {
-	expected := "00000000000000000000000000000000000000000000000000000000436f72650304560000000000000123"
+	expected := "00000000000000000000000000000000000000000000000000000000436f72650304560000000000000000000000000000000000000000000000000000000000000123"
 	bodyCoreBridgeSetMessageFee := BodyCoreBridgeSetMessageFee{
 		ChainID:    0x456,
-		MessageFee: uint64(0x123),
+		MessageFee: uint256.NewInt(0x123),
 	}
 	buf, err := bodyCoreBridgeSetMessageFee.Serialize()
 	require.NoError(t, err)