Skip to content

Commit

Permalink
add msg flags to rpc receipt type (#1364)
Browse files Browse the repository at this point in the history
  • Loading branch information
olegrok authored Dec 5, 2024
2 parents 5d2978b + 854a190 commit bfd054f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 31 deletions.
4 changes: 4 additions & 0 deletions nil/internal/types/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type MessageFlags struct {
BitFlags[uint8]
}

func NewMessageFlagsFromBits(bits uint8) MessageFlags {
return MessageFlags{BitFlags: BitFlags[uint8]{Bits: bits}}
}

func (flags MessageFlags) Value() (driver.Value, error) {
return flags.Bits, nil
}
Expand Down
54 changes: 30 additions & 24 deletions nil/services/rpc/jsonrpc/eth_receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,58 @@ type SuiteEthReceipt struct {
db db.DB
api *APIImpl
receipt types.Receipt
message types.Message
outMessages []*types.Message
}

func (suite *SuiteEthReceipt) SetupSuite() {
func (s *SuiteEthReceipt) SetupSuite() {
ctx := context.Background()

var err error
suite.db, err = db.NewBadgerDbInMemory()
suite.Require().NoError(err)
s.db, err = db.NewBadgerDbInMemory()
s.Require().NoError(err)

suite.api = NewTestEthAPI(suite.T(), ctx, suite.db, 2)
s.api = NewTestEthAPI(s.T(), ctx, s.db, 2)

tx, err := suite.db.CreateRwTx(ctx)
suite.Require().NoError(err)
tx, err := s.db.CreateRwTx(ctx)
s.Require().NoError(err)
defer tx.Rollback()

message := types.Message{Data: []byte{}, To: types.GenerateRandomAddress(types.BaseShardId)}
suite.receipt = types.Receipt{MsgHash: message.Hash(), Logs: []*types.Log{}, OutMsgIndex: 0, OutMsgNum: 2}
s.message = types.Message{
Data: []byte{},
To: types.GenerateRandomAddress(types.BaseShardId),
Flags: types.NewMessageFlags(1, 5, 7),
}
s.receipt = types.Receipt{MsgHash: s.message.Hash(), Logs: []*types.Log{}, OutMsgIndex: 0, OutMsgNum: 2}

suite.outMessages = append(suite.outMessages, &types.Message{Data: []byte{12}})
suite.outMessages = append(suite.outMessages, &types.Message{Data: []byte{34}})
s.outMessages = append(s.outMessages, &types.Message{Data: []byte{12}})
s.outMessages = append(s.outMessages, &types.Message{Data: []byte{34}})

blockHash := writeTestBlock(suite.T(), tx, types.BaseShardId, types.BlockNumber(0), []*types.Message{&message},
[]*types.Receipt{&suite.receipt}, suite.outMessages)
blockHash := writeTestBlock(s.T(), tx, types.BaseShardId, types.BlockNumber(0), []*types.Message{&s.message},
[]*types.Receipt{&s.receipt}, s.outMessages)
_, err = execution.PostprocessBlock(tx, types.BaseShardId, types.NewValueFromUint64(10), blockHash)
suite.Require().NoError(err)
s.Require().NoError(err)

err = tx.Commit()
suite.Require().NoError(err)
s.Require().NoError(err)
}

func (suite *SuiteEthReceipt) TearDownSuite() {
suite.db.Close()
func (s *SuiteEthReceipt) TearDownSuite() {
s.db.Close()
}

func (suite *SuiteEthReceipt) TestGetMessageReceipt() {
data, err := suite.api.GetInMessageReceipt(context.Background(), suite.receipt.MsgHash)
suite.Require().NoError(err)
suite.Require().NotNil(data)
func (s *SuiteEthReceipt) TestGetMessageReceipt() {
data, err := s.api.GetInMessageReceipt(context.Background(), s.receipt.MsgHash)
s.Require().NoError(err)
s.Require().NotNil(data)

for i, outMsg := range suite.outMessages {
suite.Equal(outMsg.Hash(), data.OutMessages[i])
for i, outMsg := range s.outMessages {
s.Equal(outMsg.Hash(), data.OutMessages[i])
}

suite.Equal(suite.receipt.MsgHash, data.MsgHash)
suite.Equal(suite.receipt.Success, data.Success)
s.Equal(s.receipt.MsgHash, data.MsgHash)
s.Equal(s.receipt.Success, data.Success)
s.Equal(s.message.Flags, data.Flags)
}

func TestSuiteEthReceipt(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion nil/services/rpc/jsonrpc/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ func EncodeRawBlockWithExtractedData(block *types.RawBlockWithExtractedData) (*D
// @componentprop Status status string false "Status shows concrete error of the executed message."
// @componentprop Temporary temporary boolean false "The flag that shows whether the message is temporary."
// @componentprop ErrorMessage errorMessage string false "The error in case the message processing was unsuccessful."
// @componentprop Flags flags string true "The array of message flags."
type RPCReceipt struct {
Flags types.MessageFlags `json:"flags"`
Success bool `json:"success"`
Status string `json:"status"`
FailedPc uint `json:"failedPc"`
Expand Down Expand Up @@ -347,6 +349,7 @@ func NewRPCReceipt(info *rawapitypes.ReceiptInfo) (*RPCReceipt, error) {
}

res := &RPCReceipt{
Flags: info.Flags,
Success: receipt.Success,
Status: receipt.Status.String(),
FailedPc: uint(receipt.FailedPc),
Expand All @@ -362,7 +365,7 @@ func NewRPCReceipt(info *rawapitypes.ReceiptInfo) (*RPCReceipt, error) {
BlockHash: info.BlockHash,
BlockNumber: info.BlockId,
MsgIndex: info.Index,
ShardId: info.ShardId,
ShardId: types.ShardIdFromHash(receipt.MsgHash),
Temporary: info.Temporary,
ErrorMessage: info.ErrorMessage,
IncludedInMain: info.IncludedInMain,
Expand Down
12 changes: 11 additions & 1 deletion nil/services/rpc/rawapi/local_receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (api *LocalShardApi) GetInMessageReceipt(ctx context.Context, hash common.H
}

var receipt *types.Receipt
var message *types.Message
var gasPrice types.Value

includedInMain := false
Expand All @@ -35,6 +36,10 @@ func (api *LocalShardApi) GetInMessageReceipt(ctx context.Context, hash common.H
if err != nil && !errors.Is(err, db.ErrKeyNotFound) {
return nil, err
}
message, err = getBlockEntity[*types.Message](tx, api.ShardId, db.MessageTrieTable, block.InMessagesRoot, indexes.MessageIndex.Bytes())
if err != nil && !errors.Is(err, db.ErrKeyNotFound) {
return nil, err
}
gasPrice = block.GasPrice

// Check if the message is included in the main chain
Expand Down Expand Up @@ -108,6 +113,11 @@ func (api *LocalShardApi) GetInMessageReceipt(ctx context.Context, hash common.H
}
}

var flags types.MessageFlags
if message != nil {
flags = message.Flags
}

var blockId types.BlockNumber
var blockHash common.Hash
if block != nil {
Expand All @@ -116,8 +126,8 @@ func (api *LocalShardApi) GetInMessageReceipt(ctx context.Context, hash common.H
}

return &rawapitypes.ReceiptInfo{
ShardId: api.ShardId,
ReceiptSSZ: receiptSSZ,
Flags: flags,
Index: indexes.MessageIndex,
BlockHash: blockHash,
BlockId: blockId,
Expand Down
4 changes: 2 additions & 2 deletions nil/services/rpc/rawapi/pb/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -1087,7 +1087,7 @@ func (r *ReceiptInfo) PackProtoMessage(info *rawapitypes.ReceiptInfo) *ReceiptIn
check.PanicIfErr(blockHash.PackProtoMessage(info.BlockHash))

return &ReceiptInfo{
ShardId: uint32(info.ShardId),
Flags: uint32(info.Flags.Bits),
ReceiptSSZ: info.ReceiptSSZ,
Index: uint64(info.Index),
BlockHash: blockHash,
Expand Down Expand Up @@ -1134,7 +1134,7 @@ func (r *ReceiptInfo) UnpackProtoMessage() *rawapitypes.ReceiptInfo {
blockHash, err := r.BlockHash.UnpackProtoMessage()
check.PanicIfErr(err)
return &rawapitypes.ReceiptInfo{
ShardId: types.ShardId(r.ShardId),
Flags: types.NewMessageFlagsFromBits(uint8(r.Flags)),
ReceiptSSZ: r.ReceiptSSZ,
Index: types.MessageIndex(r.Index),
BlockHash: blockHash,
Expand Down
2 changes: 1 addition & 1 deletion nil/services/rpc/rawapi/proto/message.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ message MessageResponse {
}

message ReceiptInfo {
uint32 shardId = 1;
uint32 flags = 1;
bytes receiptSSZ = 2;
uint64 index = 3;
Hash blockHash = 4;
Expand Down
3 changes: 1 addition & 2 deletions nil/services/rpc/rawapi/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ type MessageInfo struct {
}

type ReceiptInfo struct {
// TODO: we don't need shard-id as a separate field
ShardId types.ShardId
ReceiptSSZ []byte
Flags types.MessageFlags
Index types.MessageIndex
BlockHash common.Hash
BlockId types.BlockNumber
Expand Down

0 comments on commit bfd054f

Please sign in to comment.