Skip to content

Commit

Permalink
Fix the (de)serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJeremyHe committed May 28, 2024
1 parent d718ea9 commit bbbd574
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
43 changes: 36 additions & 7 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
8 changes: 4 additions & 4 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()},
},
}

Expand All @@ -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{
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand Down

0 comments on commit bbbd574

Please sign in to comment.