diff --git a/cairo/adapters/messages.go b/cairo/adapters/messages.go new file mode 100644 index 0000000..7ee83cb --- /dev/null +++ b/cairo/adapters/messages.go @@ -0,0 +1,128 @@ +package adapters + +import ( + "errors" + "fmt" + "github.com/NethermindEth/juno/core/felt" + "github.com/ethereum/go-ethereum/common" + + "math/big" +) + +type MessageL1ToL2 struct { + // The address of the L1 contract sending the message. + From common.Address `json:"from_address" validate:"required"` + // The address of the L1 contract sending the message. + To felt.Felt `json:"to_address" validate:"required"` + Nonce felt.Felt `json:"nonce" validate:"required"` + Selector felt.Felt `json:"entry_point_selector" validate:"required"` + // The payload of the message. + Payload []felt.Felt `json:"payload" validate:"required"` +} + +func (m *MessageL1ToL2) EncodeTo() ([]*big.Int, error) { + var result []*big.Int + + // From + result = append(result, new(big.Int).SetBytes(m.From.Bytes())) + + // To + if m.To.IsZero() { + return nil, errors.New("To field is zero (invalid)") + } + result = append(result, m.To.BigInt(new(big.Int))) + + // Nonce + result = append(result, m.Nonce.BigInt(new(big.Int))) + + // Selector + if m.Selector.IsZero() { + return nil, errors.New("Selector field is zero (invalid)") + } + result = append(result, m.Selector.BigInt(new(big.Int))) + + payloadSize := big.NewInt(int64(len(m.Payload))) + result = append(result, payloadSize) + // Payload + for _, p := range m.Payload { + if !p.IsZero() { + result = append(result, p.BigInt(new(big.Int))) + } + } + + return result, nil +} + +func (m *MessageL1ToL2) SizeInFelts() int { + size := 0 + size += sizeOfCommonAddress(m.From) + size += sizeOfFelt(m.To) + size += sizeOfFelt(m.Selector) + // for payload length field + size += 1 + for _, p := range m.Payload { + size += sizeOfFelt(p) + } + return size +} + +func sizeOfCommonAddress(addr common.Address) int { + return 1 +} + +func sizeOfFelt(f felt.Felt) int { + return 1 +} + +// MessageL2ToL1 L2ToL1Message +type MessageL2ToL1 struct { + From *felt.Felt `json:"from_address,omitempty"` + To common.Address `json:"to_address"` + + Payload []*felt.Felt `json:"payload"` +} + +func (m *MessageL2ToL1) EncodeTo() ([]*big.Int, error) { + var result []*big.Int + + // From + if m.From != nil { + result = append(result, m.From.BigInt(new(big.Int))) + } else { + return nil, errors.New("From field is nil") + } + + // To + result = append(result, new(big.Int).SetBytes(m.To.Bytes())) + fmt.Println("To:", new(big.Int).SetBytes(m.To.Bytes())) + + payloadSize := big.NewInt(int64(len(m.Payload))) + result = append(result, payloadSize) + // Payload + for _, p := range m.Payload { + if p != nil { + result = append(result, p.BigInt(new(big.Int))) + } + } + + return result, nil +} + +func (m *MessageL2ToL1) SizeInFelts() int { + size := 0 + + if m.From != nil { + size += sizeOfFelt(*m.From) + } + + size += sizeOfCommonAddress(m.To) + + // for payload length field + size += 1 + + for _, p := range m.Payload { + size += sizeOfFelt(*p) + } + + return size +} diff --git a/cairo/cairo.go b/cairo/cairo.go index 800c8ab..2b939f4 100644 --- a/cairo/cairo.go +++ b/cairo/cairo.go @@ -20,6 +20,7 @@ import ( "github.com/yu-org/yu/core/types" . "github.com/yu-org/yu/core/types" "github.com/yu-org/yu/utils/log" + "itachi/cairo/adapters" "itachi/cairo/config" "itachi/cairo/l1" "itachi/cairo/l1/contract" @@ -61,7 +62,6 @@ func (c *Cairo) FinalizeBlock(block *Block) { var starkReceipt *rpc.TransactionReceipt txns := block.Txns.ToArray() messagesToL1 := make([]*rpc.MsgToL1, 0) - messagesToL2 := make([]*rpc.MsgFromL1, 0) for t := 0; t < len(txns); t++ { txn := txns[t] receipt, _ := c.TxDB.GetReceipt(txn.TxnHash) @@ -74,6 +74,18 @@ func (c *Cairo) FinalizeBlock(block *Block) { messagesToL1 = append(messagesToL1, starkReceipt.MessagesSent...) } } + // Adapt + messageL2ToL1 := make([]*adapters.MessageL2ToL1, len(messagesToL1)) + for idx, msg := range messagesToL1 { + messageL2ToL1[idx] = &adapters.MessageL2ToL1{ + From: msg.From, + To: msg.To, + Payload: msg.Payload, + } + } + + // todo messagesToL2 := make([]*rpc.MsgFromL1, 0) + messagesToL2 := make([]*adapters.MessageL1ToL2, 0) num := uint64(block.Height) // init StarknetOsOutput by block @@ -84,7 +96,7 @@ func (c *Cairo) FinalizeBlock(block *Block) { BlockHash: new(felt.Felt).SetBytes(block.Hash.Bytes()), ConfigHash: new(felt.Felt).SetUint64(0), KzgDA: new(felt.Felt).SetUint64(0), - MessagesToL1: messagesToL1, + MessagesToL1: messageL2ToL1, MessagesToL2: messagesToL2, } // cairoState.UpdateStarknetOsOutput(snOsOutput) diff --git a/cairo/snos-ouput/codec.go b/cairo/snos-ouput/codec.go index 6c2e7fc..da7615d 100644 --- a/cairo/snos-ouput/codec.go +++ b/cairo/snos-ouput/codec.go @@ -3,7 +3,7 @@ package snos_ouput import ( "fmt" "github.com/NethermindEth/juno/core/felt" - "github.com/NethermindEth/juno/rpc" + "itachi/cairo/adapters" "math/big" ) @@ -23,8 +23,8 @@ type StarknetOsOutput struct { // KZG_DA KzgDA *felt.Felt `json:"kzgDA"` - MessagesToL1 []*rpc.MsgToL1 `json:"messages_to_l1"` - MessagesToL2 []*rpc.MsgFromL1 `json:"messages_to_l2"` + MessagesToL1 []*adapters.MessageL2ToL1 `json:"messages_to_l1"` + MessagesToL2 []*adapters.MessageL1ToL2 `json:"messages_to_l2"` } type SnosCodec interface {