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

Commit

Permalink
Allow overriding of ChainID
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Silas Davis committed Mar 18, 2021
1 parent 513b4c0 commit 0e91dad
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bcm/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (bc *Blockchain) GenesisDoc() genesis.GenesisDoc {
}

func (bc *Blockchain) ChainID() string {
return bc.genesisDoc.ChainID()
return bc.genesisDoc.GetChainID()
}

func (bc *Blockchain) LastBlockHeight() uint64 {
Expand Down
4 changes: 2 additions & 2 deletions cmd/burrow/commands/config_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ func (opts *configOptions) obtainBurrowConfig() (*config.BurrowConfig, error) {
}
if *opts.initMonikerOpt == "" {
chainIDHeader := ""
if conf.GenesisDoc != nil && conf.GenesisDoc.ChainID() != "" {
chainIDHeader = conf.GenesisDoc.ChainID() + "_"
if conf.GenesisDoc != nil && conf.GenesisDoc.GetChainID() != "" {
chainIDHeader = conf.GenesisDoc.GetChainID() + "_"
}
if conf.ValidatorAddress != nil {
// Set a default moniker... since we can at this stage of config completion and it is required for start
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"

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

var burrowConfigSchema = jsonschema.Reflect(&BurrowConfig{})

func DefaultBurrowConfig() *BurrowConfig {
return &BurrowConfig{
BurrowDir: ".burrow",
Expand Down
9 changes: 7 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package config

import (
"fmt"
"testing"

"github.com/hyperledger/burrow/config/source"
"github.com/hyperledger/burrow/genesis"
"github.com/stretchr/testify/require"
)

func TestBurrowConfigSerialise(t *testing.T) {
Expand All @@ -13,5 +14,9 @@ func TestBurrowConfigSerialise(t *testing.T) {
ChainName: "Foo",
},
}
fmt.Println(conf.JSONString())
confOut := new(BurrowConfig)
jsonString := conf.JSONString()
err := source.FromJSONString(jsonString, confOut)
require.NoError(t, err)
require.Equal(t, jsonString, confOut.JSONString())
}
2 changes: 1 addition & 1 deletion consensus/tendermint/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func DeriveGenesisDoc(burrowGenesisDoc *genesis.GenesisDoc, appHash []byte) *tmT
consensusParams.Block.TimeIotaMs = 1

return &tmTypes.GenesisDoc{
ChainID: burrowGenesisDoc.ChainID(),
ChainID: burrowGenesisDoc.GetChainID(),
GenesisTime: burrowGenesisDoc.GenesisTime,
Validators: validators,
AppHash: appHash,
Expand Down
10 changes: 5 additions & 5 deletions execution/exec/stream_event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
var genesisDoc, accounts, _ = genesis.NewDeterministicGenesis(345234523).GenesisDoc(10, 0)

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

stack := new(TxStack)
var txeOut *TxExecution
Expand All @@ -38,17 +38,17 @@ func TestConsumeBlockExecution(t *testing.T) {
height := int64(234242)
be := &BlockExecution{
Header: &tmproto.Header{
ChainID: genesisDoc.ChainID(),
ChainID: genesisDoc.GetChainID(),
AppHash: crypto.Keccak256([]byte("hashily")),
Time: time.Now(),
Height: height,
},
Height: uint64(height),
}
be.AppendTxs(
NewTxExecution(txs.Enclose(genesisDoc.ChainID(), newCallTx(0, 3))),
NewTxExecution(txs.Enclose(genesisDoc.ChainID(), newCallTx(0, 2))),
NewTxExecution(txs.Enclose(genesisDoc.ChainID(), newCallTx(2, 1))),
NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 3))),
NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(0, 2))),
NewTxExecution(txs.Enclose(genesisDoc.GetChainID(), newCallTx(2, 1))),
)

stack := NewBlockAccumulator()
Expand Down
2 changes: 1 addition & 1 deletion execution/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type Params struct {

func ParamsFromGenesis(genesisDoc *genesis.GenesisDoc) Params {
return Params{
ChainID: genesisDoc.ChainID(),
ChainID: genesisDoc.GetChainID(),
ProposalThreshold: genesisDoc.Params.ProposalThreshold,
}
}
Expand Down
2 changes: 1 addition & 1 deletion execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var logger = logging.NewNoopLogger()
var deterministicGenesis = genesis.NewDeterministicGenesis(34059836243380576)
var testGenesisDoc, testPrivAccounts, _ = deterministicGenesis.
GenesisDoc(3, 1)
var testChainID = testGenesisDoc.ChainID()
var testChainID = testGenesisDoc.GetChainID()

func TestSendFails(t *testing.T) {
stateDB, err := dbm.NewDB("state", dbBackend, dbDir)
Expand Down
23 changes: 12 additions & 11 deletions genesis/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,18 @@ type params struct {
}

type GenesisDoc struct {
GenesisTime time.Time
ChainName string
AppHash binary.HexBytes `json:",omitempty" toml:",omitempty"`
Params params `json:",omitempty" toml:",omitempty"`
Salt []byte `json:",omitempty" toml:",omitempty"`
GenesisTime time.Time
ChainName string
// Ordinarily we derive this from the genesis hash but to support explicit Ethereum ChainID it may be set
ChainID string `json:",omitempty" toml:",omitempty"`
AppHash binary.HexBytes
Params params `json:",omitempty" toml:",omitempty"`
Salt []byte `json:",omitempty" toml:",omitempty"`
GlobalPermissions permission.AccountPermissions
Accounts []Account
Validators []Validator
// memo
hash []byte
chainID string
hash []byte
}

func (genesisDoc *GenesisDoc) GlobalPermissionsAccount() *acm.Account {
Expand Down Expand Up @@ -115,11 +116,11 @@ func (genesisDoc *GenesisDoc) ShortHash() []byte {
return genesisDoc.Hash()[:ShortHashSuffixBytes]
}

func (genesisDoc *GenesisDoc) ChainID() string {
if genesisDoc.chainID == "" {
genesisDoc.chainID = fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
func (genesisDoc *GenesisDoc) GetChainID() string {
if genesisDoc.ChainID == "" {
genesisDoc.ChainID = fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
}
return genesisDoc.chainID
return genesisDoc.ChainID
}

//------------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions integration/governance/governance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,12 +226,12 @@ func TestGovernance(t *testing.T) {
})

setSequence(t, qcli, tx)
_, err := localSignAndBroadcastSync(t, tcli1, genesisDoc.ChainID(), genesisAccounts[0], tx)
_, err := localSignAndBroadcastSync(t, tcli1, genesisDoc.GetChainID(), genesisAccounts[0], tx)
require.NoError(t, err)

// Make it a different Tx hash so it can enter cache but keep sequence number
tx.AccountUpdates[0].Amounts = balance.New().Power(power).Native(1)
_, err = localSignAndBroadcastSync(t, tcli2, genesisDoc.ChainID(), genesisAccounts[0], tx)
_, err = localSignAndBroadcastSync(t, tcli2, genesisDoc.GetChainID(), genesisAccounts[0], tx)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid sequence")
})
Expand Down
2 changes: 1 addition & 1 deletion integration/rpcinfo/info_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestInfoServer(t *testing.T) {
resp, err := infoclient.Status(rpcClient)
require.NoError(t, err)
assert.Contains(t, resp.GetNodeInfo().GetMoniker(), "node")
assert.Equal(t, rpctest.GenesisDoc.ChainID(), resp.NodeInfo.Network,
assert.Equal(t, rpctest.GenesisDoc.GetChainID(), resp.NodeInfo.Network,
"ChainID should match NodeInfo.Network")
})

Expand Down
2 changes: 1 addition & 1 deletion integration/rpcquery/query_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestQueryServer(t *testing.T) {
status, err := cli.Status(context.Background(), &rpcquery.StatusParam{})
require.NoError(t, err)
assert.Equal(t, rpctest.PrivateAccounts[0].GetPublicKey(), status.ValidatorInfo.PublicKey)
assert.Equal(t, rpctest.GenesisDoc.ChainID(), status.ChainID)
assert.Equal(t, rpctest.GenesisDoc.GetChainID(), status.ChainID)
for i := 0; i < 3; i++ {
// Unless we get lucky this is an error
_, err = cli.Status(context.Background(), &rpcquery.StatusParam{
Expand Down
2 changes: 1 addition & 1 deletion integration/rpctest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func MakeDefaultCallTx(t *testing.T, client rpc.Client, addr *crypto.Address, co
fee uint64) *txs.Envelope {
sequence := GetSequence(t, client, PrivateAccounts[0].GetAddress())
tx := payload.NewCallTxWithSequence(PrivateAccounts[0].GetPublicKey(), addr, code, amt, gasLim, fee, sequence+1)
txEnv := txs.Enclose(GenesisDoc.ChainID(), tx)
txEnv := txs.Enclose(GenesisDoc.GetChainID(), tx)
require.NoError(t, txEnv.Sign(PrivateAccounts[0]))
return txEnv
}
Expand Down
4 changes: 2 additions & 2 deletions integration/rpctransact/transact_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestTransactServer(t *testing.T) {
})
require.NoError(t, err)
amount := uint64(2123)
txEnv := txs.Enclose(rpctest.GenesisDoc.ChainID(), &payload.SendTx{
txEnv := txs.Enclose(rpctest.GenesisDoc.GetChainID(), &payload.SendTx{
Inputs: []*payload.TxInput{{
Address: inputAddress,
Sequence: acc.Sequence + 1,
Expand Down Expand Up @@ -120,7 +120,7 @@ func TestTransactServer(t *testing.T) {
// We should see the sign bytes embedded
if !assert.Contains(t, string(bs), fmt.Sprintf("{\"ChainID\":\"%s\",\"Type\":\"CallTx\","+
"\"Payload\":{\"Input\":{\"Address\":\"E80BB91C2F0F4C3C39FC53E89BF8416B219BE6E4\",\"Amount\":230},"+
"\"Data\":\"0203060403\"}}", rpctest.GenesisDoc.ChainID())) {
"\"Data\":\"0203060403\"}}", rpctest.GenesisDoc.GetChainID())) {
fmt.Println(string(bs))
}
})
Expand Down
29 changes: 16 additions & 13 deletions rpc/web3/eth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type EthService struct {
keyClient keys.KeyClient
keyStore *keys.FilesystemKeyStore
config *tmConfig.Config
chainID *big.Int
logger *logging.Logger
}

Expand All @@ -63,16 +64,18 @@ func NewEthService(
keyClient := keys.NewLocalKeyClient(keyStore, logger)

return &EthService{
accounts,
events,
blockchain,
validators,
nodeView,
trans,
keyClient,
keyStore,
tmConfig.DefaultConfig(),
logger,
accounts: accounts,
events: events,
blockchain: blockchain,
validators: validators,
nodeView: nodeView,
trans: trans,
keyClient: keyClient,
keyStore: keyStore,
config: tmConfig.DefaultConfig(),
// Ethereum expects ChainID to be an integer value
chainID: crypto.GetEthChainID(blockchain.ChainID()),
logger: logger,
}
}

Expand Down Expand Up @@ -119,7 +122,7 @@ func (srv *EthService) NetPeerCount() (*NetPeerCountResult, error) {
// this is typically a small int (where 1 == Ethereum mainnet)
func (srv *EthService) NetVersion() (*NetVersionResult, error) {
return &NetVersionResult{
ChainID: crypto.GetEthChainID(srv.blockchain.ChainID()).String(),
ChainID: HexEncoder.BigInt(srv.chainID),
}, nil
}

Expand All @@ -133,7 +136,7 @@ func (srv *EthService) EthProtocolVersion() (*EthProtocolVersionResult, error) {
// EthChainId returns the chainID
func (srv *EthService) EthChainId() (*EthChainIdResult, error) {
return &EthChainIdResult{
ChainId: srv.blockchain.ChainID(),
ChainId: HexEncoder.BigInt(srv.chainID),
}, nil
}

Expand Down Expand Up @@ -568,7 +571,7 @@ func (srv *EthService) EthSendRawTransaction(req *EthSendRawTransactionParams) (
return nil, d.Err()
}

rawTx := txs.NewEthRawTx(srv.blockchain.ChainID())
rawTx := txs.NewEthRawTx(srv.chainID)
err := rlp.Decode(data, rawTx)
if err != nil {
return nil, err
Expand Down
18 changes: 10 additions & 8 deletions rpc/web3/eth_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ import (
"testing"
"time"

"github.com/hyperledger/burrow/rpc/web3"
"github.com/hyperledger/burrow/txs"
"github.com/hyperledger/burrow/txs/payload"

"github.com/hyperledger/burrow/acm/balance"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/execution/evm/abi"
Expand All @@ -20,17 +16,23 @@ import (
"github.com/hyperledger/burrow/logging"
"github.com/hyperledger/burrow/project"
"github.com/hyperledger/burrow/rpc"
"github.com/hyperledger/burrow/rpc/web3"
"github.com/hyperledger/burrow/txs"
"github.com/hyperledger/burrow/txs/payload"
"github.com/stretchr/testify/require"
)

var d = new(web3.HexDecoder).Must()

// Check we can force set a decimal ChainID
const chainID = "15321"

func TestWeb3Service(t *testing.T) {
ctx := context.Background()
genesisAccounts := integration.MakePrivateAccounts("burrow", 1)
genesisAccounts = append(genesisAccounts, integration.MakeEthereumAccounts("ethereum", 3)...)
genesisDoc := integration.TestGenesisDoc(genesisAccounts, 0)

genesisDoc.ChainID = chainID
config, _ := integration.NewTestConfig(genesisDoc)
logger := logging.NewNoopLogger()
kern, err := integration.TestKernel(genesisAccounts[0], genesisAccounts, config)
Expand Down Expand Up @@ -91,7 +93,7 @@ func TestWeb3Service(t *testing.T) {
t.Run("NetVersion", func(t *testing.T) {
result, err := eth.NetVersion()
require.NoError(t, err)
require.Equal(t, crypto.GetEthChainID(genesisDoc.ChainID()).String(), result.ChainID)
require.Equal(t, web3.HexEncoder.BigInt(crypto.GetEthChainID(genesisDoc.GetChainID())), result.ChainID)
})

t.Run("EthProtocolVersion", func(t *testing.T) {
Expand All @@ -104,7 +106,7 @@ func TestWeb3Service(t *testing.T) {
result, err := eth.EthChainId()
require.NoError(t, err)
doc := config.GenesisDoc
require.Equal(t, doc.ChainID(), result.ChainId)
require.Equal(t, web3.HexEncoder.BigInt(crypto.GetEthChainID(doc.GetChainID())), result.ChainId)
})
})

Expand All @@ -118,7 +120,7 @@ func TestWeb3Service(t *testing.T) {
before := acc.GetBalance()

t.Run("EthSendRawTransaction", func(t *testing.T) {
txEnv := txs.Enclose(genesisDoc.ChainID(), &payload.CallTx{
txEnv := txs.Enclose(chainID, &payload.CallTx{
Input: &payload.TxInput{
Address: sender.GetAddress(),
Amount: 1,
Expand Down
2 changes: 1 addition & 1 deletion rpc/web3/hex.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (e *hexEncoder) BytesTrim(bs []byte) string {
return "0x" + str
}

func (e *hexEncoder) BigInt(x big.Int) string {
func (e *hexEncoder) BigInt(x *big.Int) string {
return e.BytesTrim(x.Bytes())
}

Expand Down
4 changes: 2 additions & 2 deletions txs/ethereum_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type EthRawTx struct {
chainID *big.Int
}

func NewEthRawTx(chainID string) *EthRawTx {
return &EthRawTx{chainID: crypto.GetEthChainID(chainID)}
func NewEthRawTx(chainID *big.Int) *EthRawTx {
return &EthRawTx{chainID: chainID}
}

func EthRawTxFromEnvelope(txEnv *Envelope) (*EthRawTx, error) {
Expand Down

0 comments on commit 0e91dad

Please sign in to comment.