From bbbd574509f5aadbfa3b64a4edaf1ae3743136fe Mon Sep 17 00:00:00 2001 From: ImJeremyHe Date: Tue, 28 May 2024 16:15:11 +0800 Subject: [PATCH] Fix the (de)serialization --- types/types.go | 43 ++++++++++++++++++++++++++++++++++++------- types/types_test.go | 8 ++++---- 2 files changed, 40 insertions(+), 11 deletions(-) diff --git a/types/types.go b/types/types.go index 4ccf3fa..0b750ce 100644 --- a/types/types.go +++ b/types/types.go @@ -192,16 +192,16 @@ func (i *EitherChainConfig) MarshalJSON() ([]byte, error) { } type ChainConfig struct { - ChainId U256 `json:"chain_id"` - MaxBlockSize uint64 `json:"max_block_size"` - BaseFee U256 `json:"base_fee"` + ChainId U256Decimal `json:"chain_id"` + MaxBlockSize uint64 `json:"max_block_size"` + BaseFee U256Decimal `json:"base_fee"` } func (self *ChainConfig) Commit() Commitment { return NewRawCommitmentBuilder("CHAIN_CONFIG"). - Uint256Field("chain_id", &self.ChainId). + Uint256Field("chain_id", self.ChainId.ToU256()). Uint64Field("max_block_size", self.MaxBlockSize). - Uint256Field("base_fee", &self.BaseFee). + Uint256Field("base_fee", self.BaseFee.ToU256()). Finalize() } @@ -353,6 +353,31 @@ func (b *Bytes) UnmarshalJSON(in []byte) error { return nil } +// A readable decimal format for U256. Please use the struct `U256“ to initialize +// the number first and use the `ToDecimal` to convert. +type U256Decimal struct { + big.Int +} + +func (i U256Decimal) MarshalJSON() ([]byte, error) { + return json.Marshal(i.Text(10)) +} + +func (i *U256Decimal) UnmarshalJSON(in []byte) error { + var s string + if err := json.Unmarshal(in, &s); err != nil { + return err + } + if _, err := fmt.Sscanf(s, "%d", &i.Int); err != nil { + return err + } + return nil +} + +func (i *U256Decimal) ToU256() *U256 { + return &U256{i.Int} +} + // A BigInt type which serializes to JSON a a hex string. This ensures compatibility with the // Espresso APIs. type U256 struct { @@ -397,14 +422,18 @@ func (i *U256) UnmarshalJSON(in []byte) error { return nil } +func (i *U256) ToDecimal() *U256Decimal { + return &U256Decimal{i.Int} +} + type FeeInfo struct { Account common.Address `json:"account"` - Amount U256 `json:"amount"` + Amount U256Decimal `json:"amount"` } func (self *FeeInfo) Commit() Commitment { return NewRawCommitmentBuilder("FEE_INFO"). FixedSizeField("account", self.Account.Bytes()). - Uint256Field("amount", &self.Amount). + Uint256Field("amount", self.Amount.ToU256()). Finalize() } diff --git a/types/types_test.go b/types/types_test.go index e0dbaac..59913f5 100644 --- a/types/types_test.go +++ b/types/types_test.go @@ -32,7 +32,7 @@ var ReferenceNsTable NsTable = NsTable{ var ReferenceChainConfig = &ResolvableChainConfig{ EitherChainConfig{ - Left: &ChainConfig{ChainId: *NewU256().SetUint64(0x8a19), MaxBlockSize: 10240, BaseFee: *NewU256().SetUint64(0)}, + Left: &ChainConfig{ChainId: *NewU256().SetUint64(0x8a19).ToDecimal(), MaxBlockSize: 10240, BaseFee: *NewU256().SetUint64(0).ToDecimal()}, }, } @@ -52,7 +52,7 @@ var ReferenceHeader Header = Header{ NsTable: &ReferenceNsTable, BlockMerkleTreeRoot: ReferenceBlockMerkleTreeRoot, FeeMerkleTreeRoot: ReferenceFeeMerkleTreeRoot, - FeeInfo: &FeeInfo{Account: common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"), Amount: *NewU256().SetUint64(0)}, + FeeInfo: &FeeInfo{Account: common.HexToAddress("0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"), Amount: *NewU256().SetUint64(0).ToDecimal()}, } var ReferenceTransaction Transaction = Transaction{ @@ -86,7 +86,7 @@ func TestEspressoTypesL1BLockInfoJson(t *testing.T) { func TestEspressoTypesHeaderJson(t *testing.T) { data := []byte(removeWhitespace(`{ - "chain_config": { "chain_config": { "Left": { "chain_id": "0x8a19", "max_block_size": 10240, "base_fee": "0x0" } } }, + "chain_config": { "chain_config": { "Left": { "chain_id": "35353", "max_block_size": 10240, "base_fee": "0" } } }, "height": 42, "timestamp": 789, "l1_head": 124, @@ -102,7 +102,7 @@ func TestEspressoTypesHeaderJson(t *testing.T) { }, "block_merkle_tree_root": "MERKLE_COMM~yB4_Aqa35_PoskgTpcCR1oVLh6BUdLHIs7erHKWi-usUAAAAAAAAAAEAAAAAAAAAJg", "fee_merkle_tree_root": "MERKLE_COMM~VJ9z239aP9GZDrHp3VxwPd_0l28Hc5KEAB1pFeCIxhYgAAAAAAAAAAIAAAAAAAAAdA", - "fee_info":{"account":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","amount":"0x0"} + "fee_info":{"account":"0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266","amount":"0"} }`)) // Check encoding.