Skip to content
This repository was archived by the owner on May 13, 2022. It is now read-only.

Commit 0e91dad

Browse files
author
Silas Davis
committed
Allow overriding of ChainID
Also fix encoding of ChainID return values in web3 This allows integration with eth tooling like metamask that expects a numeric ChainID serialised in base 10 to a string in order to sign transactions Signed-off-by: Silas Davis <[email protected]>
1 parent 513b4c0 commit 0e91dad

File tree

18 files changed

+69
-55
lines changed

18 files changed

+69
-55
lines changed

bcm/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func (bc *Blockchain) GenesisDoc() genesis.GenesisDoc {
183183
}
184184

185185
func (bc *Blockchain) ChainID() string {
186-
return bc.genesisDoc.ChainID()
186+
return bc.genesisDoc.GetChainID()
187187
}
188188

189189
func (bc *Blockchain) LastBlockHeight() uint64 {

cmd/burrow/commands/config_options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ func (opts *configOptions) obtainBurrowConfig() (*config.BurrowConfig, error) {
115115
}
116116
if *opts.initMonikerOpt == "" {
117117
chainIDHeader := ""
118-
if conf.GenesisDoc != nil && conf.GenesisDoc.ChainID() != "" {
119-
chainIDHeader = conf.GenesisDoc.ChainID() + "_"
118+
if conf.GenesisDoc != nil && conf.GenesisDoc.GetChainID() != "" {
119+
chainIDHeader = conf.GenesisDoc.GetChainID() + "_"
120120
}
121121
if conf.ValidatorAddress != nil {
122122
// Set a default moniker... since we can at this stage of config completion and it is required for start

config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55

6+
"github.com/alecthomas/jsonschema"
67
"github.com/hyperledger/burrow/config/source"
78
"github.com/hyperledger/burrow/consensus/tendermint"
89
"github.com/hyperledger/burrow/crypto"
@@ -32,6 +33,8 @@ type BurrowConfig struct {
3233
Logging *logconfig.LoggingConfig `json:",omitempty" toml:",omitempty"`
3334
}
3435

36+
var burrowConfigSchema = jsonschema.Reflect(&BurrowConfig{})
37+
3538
func DefaultBurrowConfig() *BurrowConfig {
3639
return &BurrowConfig{
3740
BurrowDir: ".burrow",

config/config_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package config
22

33
import (
4-
"fmt"
54
"testing"
65

6+
"github.com/hyperledger/burrow/config/source"
77
"github.com/hyperledger/burrow/genesis"
8+
"github.com/stretchr/testify/require"
89
)
910

1011
func TestBurrowConfigSerialise(t *testing.T) {
@@ -13,5 +14,9 @@ func TestBurrowConfigSerialise(t *testing.T) {
1314
ChainName: "Foo",
1415
},
1516
}
16-
fmt.Println(conf.JSONString())
17+
confOut := new(BurrowConfig)
18+
jsonString := conf.JSONString()
19+
err := source.FromJSONString(jsonString, confOut)
20+
require.NoError(t, err)
21+
require.Equal(t, jsonString, confOut.JSONString())
1722
}

consensus/tendermint/tendermint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func DeriveGenesisDoc(burrowGenesisDoc *genesis.GenesisDoc, appHash []byte) *tmT
102102
consensusParams.Block.TimeIotaMs = 1
103103

104104
return &tmTypes.GenesisDoc{
105-
ChainID: burrowGenesisDoc.ChainID(),
105+
ChainID: burrowGenesisDoc.GetChainID(),
106106
GenesisTime: burrowGenesisDoc.GenesisTime,
107107
Validators: validators,
108108
AppHash: appHash,

execution/exec/stream_event_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
var genesisDoc, accounts, _ = genesis.NewDeterministicGenesis(345234523).GenesisDoc(10, 0)
1818

1919
func TestTxExecution(t *testing.T) {
20-
txe := NewTxExecution(txs.Enclose(genesisDoc.ChainID(), newCallTx(0, 1)))
20+
txe := NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 1)))
2121

2222
stack := new(TxStack)
2323
var txeOut *TxExecution
@@ -38,17 +38,17 @@ func TestConsumeBlockExecution(t *testing.T) {
3838
height := int64(234242)
3939
be := &BlockExecution{
4040
Header: &tmproto.Header{
41-
ChainID: genesisDoc.ChainID(),
41+
ChainID: genesisDoc.GetChainID(),
4242
AppHash: crypto.Keccak256([]byte("hashily")),
4343
Time: time.Now(),
4444
Height: height,
4545
},
4646
Height: uint64(height),
4747
}
4848
be.AppendTxs(
49-
NewTxExecution(txs.Enclose(genesisDoc.ChainID(), newCallTx(0, 3))),
50-
NewTxExecution(txs.Enclose(genesisDoc.ChainID(), newCallTx(0, 2))),
51-
NewTxExecution(txs.Enclose(genesisDoc.ChainID(), newCallTx(2, 1))),
49+
NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 3))),
50+
NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 2))),
51+
NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(2, 1))),
5252
)
5353

5454
stack := NewBlockAccumulator()

execution/execution.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ type Params struct {
9797

9898
func ParamsFromGenesis(genesisDoc *genesis.GenesisDoc) Params {
9999
return Params{
100-
ChainID: genesisDoc.ChainID(),
100+
ChainID: genesisDoc.GetChainID(),
101101
ProposalThreshold: genesisDoc.Params.ProposalThreshold,
102102
}
103103
}

execution/execution_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var logger = logging.NewNoopLogger()
5353
var deterministicGenesis = genesis.NewDeterministicGenesis(34059836243380576)
5454
var testGenesisDoc, testPrivAccounts, _ = deterministicGenesis.
5555
GenesisDoc(3, 1)
56-
var testChainID = testGenesisDoc.ChainID()
56+
var testChainID = testGenesisDoc.GetChainID()
5757

5858
func TestSendFails(t *testing.T) {
5959
stateDB, err := dbm.NewDB("state", dbBackend, dbDir)

genesis/genesis.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@ type params struct {
5454
}
5555

5656
type GenesisDoc struct {
57-
GenesisTime time.Time
58-
ChainName string
59-
AppHash binary.HexBytes `json:",omitempty" toml:",omitempty"`
60-
Params params `json:",omitempty" toml:",omitempty"`
61-
Salt []byte `json:",omitempty" toml:",omitempty"`
57+
GenesisTime time.Time
58+
ChainName string
59+
// Ordinarily we derive this from the genesis hash but to support explicit Ethereum ChainID it may be set
60+
ChainID string `json:",omitempty" toml:",omitempty"`
61+
AppHash binary.HexBytes
62+
Params params `json:",omitempty" toml:",omitempty"`
63+
Salt []byte `json:",omitempty" toml:",omitempty"`
6264
GlobalPermissions permission.AccountPermissions
6365
Accounts []Account
6466
Validators []Validator
6567
// memo
66-
hash []byte
67-
chainID string
68+
hash []byte
6869
}
6970

7071
func (genesisDoc *GenesisDoc) GlobalPermissionsAccount() *acm.Account {
@@ -115,11 +116,11 @@ func (genesisDoc *GenesisDoc) ShortHash() []byte {
115116
return genesisDoc.Hash()[:ShortHashSuffixBytes]
116117
}
117118

118-
func (genesisDoc *GenesisDoc) ChainID() string {
119-
if genesisDoc.chainID == "" {
120-
genesisDoc.chainID = fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
119+
func (genesisDoc *GenesisDoc) GetChainID() string {
120+
if genesisDoc.ChainID == "" {
121+
genesisDoc.ChainID = fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
121122
}
122-
return genesisDoc.chainID
123+
return genesisDoc.ChainID
123124
}
124125

125126
//------------------------------------------------------------

integration/governance/governance_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ func TestGovernance(t *testing.T) {
226226
})
227227

228228
setSequence(t, qcli, tx)
229-
_, err := localSignAndBroadcastSync(t, tcli1, genesisDoc.ChainID(), genesisAccounts[0], tx)
229+
_, err := localSignAndBroadcastSync(t, tcli1, genesisDoc.GetChainID(), genesisAccounts[0], tx)
230230
require.NoError(t, err)
231231

232232
// Make it a different Tx hash so it can enter cache but keep sequence number
233233
tx.AccountUpdates[0].Amounts = balance.New().Power(power).Native(1)
234-
_, err = localSignAndBroadcastSync(t, tcli2, genesisDoc.ChainID(), genesisAccounts[0], tx)
234+
_, err = localSignAndBroadcastSync(t, tcli2, genesisDoc.GetChainID(), genesisAccounts[0], tx)
235235
require.Error(t, err)
236236
assert.Contains(t, err.Error(), "invalid sequence")
237237
})

0 commit comments

Comments
 (0)