Skip to content

Commit 6038634

Browse files
authored
Merge pull request #977 from NilFoundation/beneficiary
feat: add coinbase address to block structure and RPC
2 parents 43d19ea + b83d59b commit 6038634

8 files changed

Lines changed: 38 additions & 4 deletions

File tree

nil/internal/execution/state.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ const (
3838
ModeSyncReplay = "syncer-replay"
3939
ModeManualReplay = "manual-replay"
4040
ModeVerify = "verify"
41+
42+
// TODO: align with protocol – currently using a dummy address to match Ethereum block structure
43+
CoinbaseShardIndependentAddr = "0x0"
4144
)
4245

4346
var blocksTracer *BlocksTracer
@@ -109,6 +112,8 @@ type ExecutionState struct {
109112

110113
GasUsed types.Gas
111114

115+
CoinbaseAddress types.Address
116+
112117
// Transient storage
113118
transientStorage transientStorage
114119

@@ -281,6 +286,7 @@ func NewEVMBlockContext(es *ExecutionState) (*vm.BlockContext, error) {
281286
BlobBaseFee: big.NewInt(10),
282287
GasLimit: es.GasLimit.Uint64(),
283288
Time: time,
289+
Coinbase: es.CoinbaseAddress,
284290

285291
RollbackCounter: rollbackCounter,
286292
}, nil
@@ -352,6 +358,7 @@ func NewExecutionState(tx db.RoTx, shardId types.ShardId, params StateParams) (*
352358
Logs: map[common.Hash][]*types.Log{},
353359
DebugLogs: map[common.Hash][]*types.DebugLog{},
354360
Errors: map[common.Hash]error{},
361+
CoinbaseAddress: types.ShardAndHexToAddress(shardId, CoinbaseShardIndependentAddr),
355362

356363
journal: newJournal(),
357364
transientStorage: newTransientStorage(),
@@ -1185,6 +1192,11 @@ func (es *ExecutionState) HandleTransaction(
11851192
default:
11861193
res = es.handleExecutionTransaction(ctx, txn)
11871194
}
1195+
1196+
if err := es.transferPriorityFee(res.GasUsed.ToValue(txn.MaxPriorityFeePerGas)); err != nil {
1197+
return NewExecutionResult().SetFatal(fmt.Errorf("transferPriorityFee failed: %w", err))
1198+
}
1199+
11881200
responseWasSent := false
11891201
bounced := false
11901202
if txn.IsRequest() {
@@ -2191,3 +2203,7 @@ func VerboseTracingHooks(logger logging.Logger) *tracing.Hooks {
21912203
},
21922204
}
21932205
}
2206+
2207+
func (es *ExecutionState) transferPriorityFee(priorityFee types.Value) error {
2208+
return es.AddBalance(es.CoinbaseAddress, priorityFee, tracing.BalanceIncreaseRewardTransactionFee)
2209+
}

nil/internal/types/block.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type BlockData struct {
5151
BaseFee Value `json:"gasPrice" ch:"gas_price"`
5252
GasUsed Gas `json:"gasUsed" ch:"gas_used"`
5353
L1BlockNumber uint64 `json:"l1BlockNumber" ch:"l1_block_number"`
54+
Coinbase Address `json:"miner" ch:"miner"`
5455

5556
// Incremented after every rollback, used to prevent rollback replay attacks
5657
RollbackCounter uint32 `json:"rollbackCounter" ch:"rollback_counter"`

nil/internal/types/serialization_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestSerializeBlock(t *testing.T) {
3434
h, err := common.Keccak(&block2)
3535
require.NoError(t, err)
3636

37-
h2, err := hex.DecodeString("24ff2a9b05d533266c08cc1906bffcd746ecd1a427255de2319f9a9a45fa1a75")
37+
h2, err := hex.DecodeString("03b723f064c70966267e9ded7d13fc07e1a082f5ea4e2156c77765cfe933c657")
3838
require.NoError(t, err)
3939

4040
require.Equal(t, common.BytesToHash(h2), common.BytesToHash(h[:]))

nil/services/cliservice/block.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Block #{{ .block.Id }} [{{ .color.bold }}{{ .block.Hash .shardId }}{{ .color.res
123123
PrevBlock: {{ .block.PrevBlock }}
124124
BaseFee: {{ .block.BaseFee }}
125125
GasUsed: {{ .block.GasUsed }}
126+
Coinbase: {{ .block.Coinbase }}
126127
ChildBlocksRootHash: {{ .block.ChildBlocksRootHash }}
127128
{{- if len .block.ChildBlocks}}
128129
ChildBlocks:

nil/services/cliservice/block_format_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,10 +147,11 @@ func TestDebugBlockToText(t *testing.T) {
147147
text, err := s.debugBlockToText(types.ShardId(13), block, false, false)
148148
require.NoError(t, err)
149149

150-
expectedText := `Block #100500 [0x000d1288cfedf4f9c748f80d9d0e3edb422c092a2a17725878e36aa9383c6bfa] @ 13 shard
150+
expectedText := `Block #100500 [0x000dcc04885a7be4c360239bfbad32edb62f486770189fe98244c43c793ae9b6] @ 13 shard
151151
PrevBlock: 0x00000000000000000000000000000000000000000000000000000000deadbeef
152152
BaseFee: 0
153153
GasUsed: 1234
154+
Coinbase: 0x0000000000000000000000000000000000000000
154155
ChildBlocksRootHash: 0x00000000000000000000000000000000000000000000000000000000deadbabe
155156
ChildBlocks:
156157
- 1: 0x0000000000000000000000000000000000000000000000000000000000000111

nil/services/rpc/jsonrpc/eth_block_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (suite *SuiteEthBlock) TestGetBlockByHash() {
9191
suite.Require().NoError(err)
9292
suite.Require().NotNil(data)
9393
suite.Equal(suite.lastBlockHash, data.Hash)
94+
suite.Equal(data.Coinbase, types.ShardAndHexToAddress(shardId, "0x0"))
9495
}
9596

9697
func (suite *SuiteEthBlock) TestGetBlockTransactionCountByHash() {

nil/services/rpc/jsonrpc/types.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ type RPCInTransaction struct {
7272
// @componentprop ParentHash parentHash string true "The hash of the parent block."
7373
// @componentprop ReceiptsRoot receiptsRoot string true "The root of the block receipts."
7474
// @componentprop ShardId shardId integer true "The ID of the shard where the block was generated."
75+
// @componentprop Coinbase miner string true "The address of the fee recipient that receives priority fees."
7576
type RPCBlock struct {
7677
Number types.BlockNumber `json:"number"`
7778
Hash common.Hash `json:"hash"`
@@ -93,6 +94,7 @@ type RPCBlock struct {
9394
L1Number uint64 `json:"l1Number"`
9495
LogsBloom hexutil.Bytes `json:"logsBloom,omitempty"`
9596
GasUsed types.Gas `json:"gasUsed,omitempty"`
97+
Coinbase types.Address `json:"miner"`
9698
}
9799

98100
type ShardCount struct {
@@ -435,6 +437,7 @@ func NewRPCBlock(shardId types.ShardId, data *BlockWithEntities, fullTx bool) (*
435437
LogsBloom: bloom,
436438
L1Number: block.L1BlockNumber,
437439
GasUsed: block.GasUsed,
440+
Coinbase: block.Coinbase,
438441
}, nil
439442
}
440443

nil/services/synccommittee/prover/tracer/internal/mpttracer/mpt_tracer.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ func (mt *MPTTracer) GetAccountState(addr types.Address, createIfNotExists bool)
5555
// try to fetch from cache
5656
smartContract, err := contractTrie.Fetch(addr.Hash())
5757
if smartContract != nil {
58-
// we fetched this contract before (in could be even updated by this time)
59-
return mt.accountsTraceableStates[addr], nil
58+
cachedAcc, exists := mt.accountsTraceableStates[addr]
59+
if exists {
60+
return cachedAcc, nil
61+
}
62+
// it could exist in the trie, but not in cache. E.g., the trie is reused between tracing
63+
return mt.createTraceableAccount(addr, smartContract)
6064
}
6165
if err != nil && !errors.Is(err, db.ErrKeyNotFound) {
6266
return nil, err
@@ -86,6 +90,13 @@ func (mt *MPTTracer) GetAccountState(addr types.Address, createIfNotExists bool)
8690
return nil, nil
8791
}
8892

93+
return mt.createTraceableAccount(addr, contract)
94+
}
95+
96+
func (mt *MPTTracer) createTraceableAccount(
97+
addr types.Address,
98+
contract *types.SmartContract,
99+
) (*TraceableAccount, error) {
89100
traceableAcc, err := NewTracableAccountState(mt, addr, contract, mt.logger)
90101
if err != nil {
91102
return nil, err

0 commit comments

Comments
 (0)