forked from hyperledger-archives/burrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
burrow-mint.go
216 lines (179 loc) · 7.02 KB
/
burrow-mint.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
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package burrowmint
import (
"bytes"
"fmt"
"sync"
"time"
abci "github.com/tendermint/abci/types"
tendermint_events "github.com/tendermint/go-events"
wire "github.com/tendermint/go-wire"
consensus_types "github.com/hyperledger/burrow/consensus/types"
"github.com/hyperledger/burrow/logging"
logging_types "github.com/hyperledger/burrow/logging/types"
sm "github.com/hyperledger/burrow/manager/burrow-mint/state"
manager_types "github.com/hyperledger/burrow/manager/types"
"github.com/hyperledger/burrow/txs"
)
//--------------------------------------------------------------------------------
// BurrowMint holds the current state, runs transactions, computes hashes.
// Typically two connections are opened by the tendermint core:
// one for mempool, one for consensus.
type BurrowMint struct {
mtx sync.Mutex
state *sm.State
cache *sm.BlockCache
checkCache *sm.BlockCache // for CheckTx (eg. so we get nonces right)
evc *tendermint_events.EventCache
evsw tendermint_events.EventSwitch
nTxs int // count txs in a block
logger logging_types.InfoTraceLogger
}
// Currently we just wrap ConsensusEngine but this interface can give us
// arbitrary control over the type of ConsensusEngine at such a point that we
// support others. For example it can demand a 'marker' function:
// func IsBurrowMint_0.XX.XX_CompatibleConsensusEngine()
type BurrowMintCompatibleConsensusEngine interface {
consensus_types.ConsensusEngine
}
// NOTE [ben] Compiler check to ensure BurrowMint successfully implements
// burrow/manager/types.Application
var _ manager_types.Application = (*BurrowMint)(nil)
func (app *BurrowMint) CompatibleConsensus(consensusEngine consensus_types.ConsensusEngine) bool {
_, ok := consensusEngine.(BurrowMintCompatibleConsensusEngine)
return ok
}
func (app *BurrowMint) GetState() *sm.State {
app.mtx.Lock()
defer app.mtx.Unlock()
return app.state.Copy()
}
// TODO: this is used for call/callcode and to get nonces during mempool.
// the former should work on last committed state only and the later should
// be handled by the client, or a separate wallet-like nonce tracker thats not part of the app
func (app *BurrowMint) GetCheckCache() *sm.BlockCache {
return app.checkCache
}
func NewBurrowMint(s *sm.State, evsw tendermint_events.EventSwitch,
logger logging_types.InfoTraceLogger) *BurrowMint {
return &BurrowMint{
state: s,
cache: sm.NewBlockCache(s),
checkCache: sm.NewBlockCache(s),
evc: tendermint_events.NewEventCache(evsw),
evsw: evsw,
logger: logging.WithScope(logger, "BurrowMint"),
}
}
// Implements manager/types.Application
func (app *BurrowMint) Info() (info abci.ResponseInfo) {
return abci.ResponseInfo{}
}
// Implements manager/types.Application
func (app *BurrowMint) SetOption(key string, value string) (log string) {
return ""
}
// Implements manager/types.Application
func (app *BurrowMint) DeliverTx(txBytes []byte) abci.Result {
app.nTxs += 1
// XXX: if we had tx ids we could cache the decoded txs on CheckTx
var n int
var err error
tx := new(txs.Tx)
buf := bytes.NewBuffer(txBytes)
wire.ReadBinaryPtr(tx, buf, len(txBytes), &n, &err)
if err != nil {
return abci.NewError(abci.CodeType_EncodingError, fmt.Sprintf("Encoding error: %v", err))
}
err = sm.ExecTx(app.cache, *tx, true, app.evc, app.logger)
if err != nil {
return abci.NewError(abci.CodeType_InternalError, fmt.Sprintf("Internal error: %v", err))
}
receipt := txs.GenerateReceipt(app.state.ChainID, *tx)
receiptBytes := wire.BinaryBytes(receipt)
return abci.NewResultOK(receiptBytes, "Success")
}
// Implements manager/types.Application
func (app *BurrowMint) CheckTx(txBytes []byte) abci.Result {
var n int
var err error
tx := new(txs.Tx)
buf := bytes.NewBuffer(txBytes)
wire.ReadBinaryPtr(tx, buf, len(txBytes), &n, &err)
if err != nil {
return abci.NewError(abci.CodeType_EncodingError, fmt.Sprintf("Encoding error: %v", err))
}
// TODO: map ExecTx errors to sensible abci error codes
err = sm.ExecTx(app.checkCache, *tx, false, nil, app.logger)
if err != nil {
return abci.NewError(abci.CodeType_InternalError, fmt.Sprintf("Internal error: %v", err))
}
receipt := txs.GenerateReceipt(app.state.ChainID, *tx)
receiptBytes := wire.BinaryBytes(receipt)
return abci.NewResultOK(receiptBytes, "Success")
}
// Implements manager/types.Application
// Commit the state (called at end of block)
// NOTE: CheckTx/AppendTx must not run concurrently with Commit -
// the mempool should run during AppendTxs, but lock for Commit and Update
func (app *BurrowMint) Commit() (res abci.Result) {
app.mtx.Lock() // the lock protects app.state
defer app.mtx.Unlock()
app.state.LastBlockHeight += 1
logging.InfoMsg(app.logger, "Committing block",
"last_block_height", app.state.LastBlockHeight)
// sync the AppendTx cache
app.cache.Sync()
// Refresh the checkCache with the latest commited state
logging.InfoMsg(app.logger, "Resetting checkCache",
"txs", app.nTxs)
app.checkCache = sm.NewBlockCache(app.state)
app.nTxs = 0
// save state to disk
app.state.Save()
// flush events to listeners (XXX: note issue with blocking)
app.evc.Flush()
// TODO: [ben] over the tendermint 0.6 TMSP interface we have
// no access to the block header implemented;
// On Tendermint v0.8 load the blockheader into the application
// state and remove the fixed 2-"seconds" per block internal clock.
// NOTE: set internal time as two seconds per block
app.state.LastBlockTime = app.state.LastBlockTime.Add(time.Duration(2) * time.Second)
appHash := app.state.Hash()
return abci.NewResultOK(appHash, "Success")
}
func (app *BurrowMint) Query(query abci.RequestQuery) (res abci.ResponseQuery) {
return abci.ResponseQuery{
Code: abci.CodeType_OK,
Log: "success",
}
}
// BlockchainAware interface
// Initialise the blockchain
// validators: genesis validators from tendermint core
func (app *BurrowMint) InitChain(validators []*abci.Validator) {
// Could verify agreement on initial validator set here
}
// Signals the beginning of a block
func (app *BurrowMint) BeginBlock(hash []byte, header *abci.Header) {
}
// Signals the end of a blockchain, return value can be used to modify validator
// set and voting power distribution see our BlockchainAware interface
func (app *BurrowMint) EndBlock(height uint64) (respEndblock abci.ResponseEndBlock) {
// TODO: [Silas] Bondage
// TODO: [Silas] this might be a better place for us to dispatch new block
// events particularly if we want to separate ourselves from go-events
return
}