-
Notifications
You must be signed in to change notification settings - Fork 508
/
blockchain_wrapper.go
219 lines (172 loc) · 7.13 KB
/
blockchain_wrapper.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package polybft
import (
"errors"
"fmt"
"math/big"
"time"
"github.com/hashicorp/go-hclog"
"github.com/0xPolygon/polygon-edge/blockchain"
"github.com/0xPolygon/polygon-edge/consensus"
"github.com/0xPolygon/polygon-edge/contracts"
"github.com/0xPolygon/polygon-edge/state"
"github.com/0xPolygon/polygon-edge/types"
"github.com/umbracle/ethgo"
"github.com/umbracle/ethgo/contract"
)
const (
consensusSource = "consensus"
)
var (
errSendTxnUnsupported = errors.New("system state does not support send transactions")
)
// blockchain is an interface that wraps the methods called on blockchain
type blockchainBackend interface {
// CurrentHeader returns the header of blockchain block head
CurrentHeader() *types.Header
// CommitBlock commits a block to the chain.
CommitBlock(block *types.FullBlock) error
// NewBlockBuilder is a factory method that returns a block builder on top of 'parent'.
NewBlockBuilder(parent *types.Header, coinbase types.Address,
txPool txPoolInterface, blockTime time.Duration, logger hclog.Logger) (blockBuilder, error)
// ProcessBlock builds a final block from given 'block' on top of 'parent'.
ProcessBlock(parent *types.Header, block *types.Block) (*types.FullBlock, error)
// GetStateProviderForBlock returns a reference to make queries to the state at 'block'.
GetStateProviderForBlock(block *types.Header) (contract.Provider, error)
// GetStateProvider returns a reference to make queries to the provided state.
GetStateProvider(transition *state.Transition) contract.Provider
// GetHeaderByNumber returns a reference to block header for the given block number.
GetHeaderByNumber(number uint64) (*types.Header, bool)
// GetHeaderByHash returns a reference to block header for the given block hash
GetHeaderByHash(hash types.Hash) (*types.Header, bool)
// GetSystemState creates a new instance of SystemState interface
GetSystemState(provider contract.Provider) SystemState
SubscribeEvents() blockchain.Subscription
// GetChainID returns chain id of the current blockchain
GetChainID() uint64
// GetReceiptsByHash retrieves receipts by hash
GetReceiptsByHash(hash types.Hash) ([]*types.Receipt, error)
}
var _ blockchainBackend = &blockchainWrapper{}
type blockchainWrapper struct {
executor *state.Executor
blockchain *blockchain.Blockchain
}
// CurrentHeader returns the header of blockchain block head
func (p *blockchainWrapper) CurrentHeader() *types.Header {
return p.blockchain.Header()
}
// CommitBlock commits a block to the chain
func (p *blockchainWrapper) CommitBlock(block *types.FullBlock) error {
return p.blockchain.WriteFullBlock(block, consensusSource)
}
// ProcessBlock builds a final block from given 'block' on top of 'parent'
func (p *blockchainWrapper) ProcessBlock(parent *types.Header, block *types.Block) (*types.FullBlock, error) {
header := block.Header.Copy()
start := time.Now().UTC()
transition, err := p.executor.BeginTxn(parent.StateRoot, header, types.BytesToAddress(header.Miner))
if err != nil {
return nil, err
}
// apply transactions from block
for _, tx := range block.Transactions {
if err = transition.Write(tx); err != nil {
return nil, fmt.Errorf("process block tx error, tx = %v, err = %w", tx.Hash, err)
}
}
_, root, err := transition.Commit()
if err != nil {
return nil, fmt.Errorf("failed to commit the state changes: %w", err)
}
updateBlockExecutionMetric(start)
if root != block.Header.StateRoot {
return nil, fmt.Errorf("incorrect state root: (%s, %s)", root, block.Header.StateRoot)
}
// build the block
builtBlock := consensus.BuildBlock(consensus.BuildBlockParams{
Header: header,
Txns: block.Transactions,
Receipts: transition.Receipts(),
})
if builtBlock.Header.TxRoot != block.Header.TxRoot {
return nil, fmt.Errorf("incorrect tx root (expected: %s, actual: %s)",
builtBlock.Header.TxRoot, block.Header.TxRoot)
}
return &types.FullBlock{
Block: builtBlock,
Receipts: transition.Receipts(),
}, nil
}
// GetStateProviderForBlock is an implementation of blockchainBackend interface
func (p *blockchainWrapper) GetStateProviderForBlock(header *types.Header) (contract.Provider, error) {
transition, err := p.executor.BeginTxn(header.StateRoot, header, types.ZeroAddress)
if err != nil {
return nil, err
}
return NewStateProvider(transition), nil
}
// GetStateProvider returns a reference to make queries to the provided state
func (p *blockchainWrapper) GetStateProvider(transition *state.Transition) contract.Provider {
return NewStateProvider(transition)
}
// GetHeaderByNumber is an implementation of blockchainBackend interface
func (p *blockchainWrapper) GetHeaderByNumber(number uint64) (*types.Header, bool) {
return p.blockchain.GetHeaderByNumber(number)
}
// GetHeaderByHash is an implementation of blockchainBackend interface
func (p *blockchainWrapper) GetHeaderByHash(hash types.Hash) (*types.Header, bool) {
return p.blockchain.GetHeaderByHash(hash)
}
// NewBlockBuilder is an implementation of blockchainBackend interface
func (p *blockchainWrapper) NewBlockBuilder(
parent *types.Header, coinbase types.Address,
txPool txPoolInterface, blockTime time.Duration, logger hclog.Logger) (blockBuilder, error) {
gasLimit, err := p.blockchain.CalculateGasLimit(parent.Number + 1)
if err != nil {
return nil, err
}
return NewBlockBuilder(&BlockBuilderParams{
BlockTime: blockTime,
Parent: parent,
Coinbase: coinbase,
Executor: p.executor,
GasLimit: gasLimit,
BaseFee: p.blockchain.CalculateBaseFee(parent),
TxPool: txPool,
Logger: logger,
}), nil
}
// GetSystemState is an implementation of blockchainBackend interface
func (p *blockchainWrapper) GetSystemState(provider contract.Provider) SystemState {
return NewSystemState(contracts.ValidatorSetContract, contracts.StateReceiverContract, provider)
}
func (p *blockchainWrapper) SubscribeEvents() blockchain.Subscription {
return p.blockchain.SubscribeEvents()
}
func (p *blockchainWrapper) GetChainID() uint64 {
return uint64(p.blockchain.Config().ChainID)
}
func (p *blockchainWrapper) GetReceiptsByHash(hash types.Hash) ([]*types.Receipt, error) {
return p.blockchain.GetReceiptsByHash(hash)
}
var _ contract.Provider = &stateProvider{}
type stateProvider struct {
transition *state.Transition
}
// NewStateProvider initializes EVM against given state and chain config and returns stateProvider instance
// which is an abstraction for smart contract calls
func NewStateProvider(transition *state.Transition) contract.Provider {
return &stateProvider{transition: transition}
}
// Call implements the contract.Provider interface to make contract calls directly to the state
func (s *stateProvider) Call(addr ethgo.Address, input []byte, opts *contract.CallOpts) ([]byte, error) {
result := s.transition.Call2(contracts.SystemCaller, types.Address(addr), input, big.NewInt(0), 10000000)
if result.Failed() {
return nil, result.Err
}
return result.ReturnValue, nil
}
// Txn is part of the contract.Provider interface to make Ethereum transactions. We disable this function
// since the system state does not make any transaction
func (s *stateProvider) Txn(ethgo.Address, ethgo.Key, []byte) (contract.Txn, error) {
return nil, errSendTxnUnsupported
}