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

Commit a1b8ac2

Browse files
author
Silas Davis
committed
Fix unstable genesis hash between v0.30.0 and v0.31.0
This was due to accidental removaal of `omitempty` from AppHash. Also: - Add simple genesis stability snapshot test - Treat `GenesisDoc.ChainID` as read only and re-introduce `GenesisDoc.chainID` as lazy memo storage for ChainID to avoid writing back a chainID which was generated and never present in verbatim gendoc Signed-off-by: Silas Davis <[email protected]>
1 parent 5913fa7 commit a1b8ac2

File tree

4 files changed

+144
-11
lines changed

4 files changed

+144
-11
lines changed

bcm/blockchain_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestLoadOrNewBlockchain(t *testing.T) {
2121
assert.False(t, exists)
2222
assert.Equal(t, genesisDoc.GenesisTime, blockchain.LastBlockTime())
2323
assert.Equal(t, uint64(0), blockchain.LastBlockHeight())
24-
assert.Equal(t, genesisDoc.Hash(), blockchain.AppHashAfterLastBlock())
24+
assert.Equal(t, genesisDoc.Hash().Bytes(), blockchain.AppHashAfterLastBlock())
2525

2626
// First block
2727
blockTime1 := genesisDoc.GenesisTime.Add(time.Second * 10)

execution/evm/abi/abi.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func LoadPath(abiFileOrDirs ...string) (*Spec, error) {
3535
return fmt.Errorf("error returned while walking abiDir '%s': %v", dir, err)
3636
}
3737
ext := filepath.Ext(path)
38-
if fi.IsDir() || !(ext == ".bin" || ext == ".abi") {
38+
if fi.IsDir() || !(ext == ".bin" || ext == ".abi" || ext == ".json") {
3939
return nil
4040
}
4141
abiSpc, err := ReadSpecFile(path)

genesis/genesis.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ type GenesisDoc struct {
5757
GenesisTime time.Time
5858
ChainName string
5959
// 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"`
60+
ChainID string `json:",omitempty" toml:",omitempty"`
61+
AppHash binary.HexBytes `json:",omitempty" toml:",omitempty"`
62+
Params params `json:",omitempty" toml:",omitempty"`
63+
Salt []byte `json:",omitempty" toml:",omitempty"`
6464
GlobalPermissions permission.AccountPermissions
6565
Accounts []Account
6666
Validators []Validator
6767
// memo
68-
hash []byte
68+
chainID string
69+
hash []byte
6970
}
7071

7172
func (genesisDoc *GenesisDoc) GlobalPermissionsAccount() *acm.Account {
@@ -98,7 +99,7 @@ func (genesisDoc *GenesisDoc) JSONBytes() ([]byte, error) {
9899
return json.MarshalIndent(genesisDoc, "", "\t")
99100
}
100101

101-
func (genesisDoc *GenesisDoc) Hash() []byte {
102+
func (genesisDoc *GenesisDoc) Hash() binary.HexBytes {
102103
if genesisDoc.hash != nil {
103104
return genesisDoc.hash
104105
}
@@ -117,10 +118,15 @@ func (genesisDoc *GenesisDoc) ShortHash() []byte {
117118
}
118119

119120
func (genesisDoc *GenesisDoc) GetChainID() string {
120-
if genesisDoc.ChainID == "" {
121-
genesisDoc.ChainID = fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
121+
if genesisDoc.chainID == "" {
122+
// Prefer explicit override ChainID
123+
if genesisDoc.ChainID != "" {
124+
genesisDoc.chainID = genesisDoc.ChainID
125+
} else {
126+
genesisDoc.chainID = fmt.Sprintf("%s-%X", genesisDoc.ChainName, genesisDoc.ShortHash())
127+
}
122128
}
123-
return genesisDoc.ChainID
129+
return genesisDoc.chainID
124130
}
125131

126132
//------------------------------------------------------------

genesis/genesis_test.go

+127
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/stretchr/testify/require"
9+
810
"github.com/hyperledger/burrow/acm"
911
"github.com/hyperledger/burrow/acm/validator"
1012
"github.com/hyperledger/burrow/permission"
@@ -33,6 +35,16 @@ func TestMakeGenesisDocFromAccounts(t *testing.T) {
3335
assert.Equal(t, genDoc.Hash(), genDocOut.Hash())
3436
fmt.Println(string(bs))
3537
}
38+
func TestGenesisStability(t *testing.T) {
39+
genDoc := MakeGenesisDocFromAccounts("test-chain", nil, genesisTime,
40+
accountMap("Tinkie-winkie", "Lala", "Po", "Dipsy"),
41+
validatorMap("Foo", "Bar", "Baz"),
42+
)
43+
44+
require.Equal(t, expectedGenesisJSON, genDoc.JSONString())
45+
46+
require.Equal(t, "C5B64E6AD231221C328271ADCE401AA11F9DF12830F7DA2FC3B2C923E929C532", genDoc.Hash().String())
47+
}
3648

3749
func accountMap(names ...string) map[string]*acm.Account {
3850
accounts := make(map[string]*acm.Account, len(names))
@@ -59,3 +71,118 @@ func accountFromName(name string) *acm.Account {
5971
ca.Permissions = permission.AllAccountPermissions.Clone()
6072
return ca
6173
}
74+
75+
// For genesis stability test
76+
const expectedGenesisJSON = `{
77+
"GenesisTime": "2017-10-27T00:00:00Z",
78+
"ChainName": "test-chain",
79+
"Params": {
80+
"ProposalThreshold": 0
81+
},
82+
"GlobalPermissions": {
83+
"Base": {
84+
"Perms": "send | call | createContract | createAccount | bond | name | proposal | input | batch | hasBase | hasRole",
85+
"SetBit": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole"
86+
}
87+
},
88+
"Accounts": [
89+
{
90+
"Address": "410427F4A361B958C97B47D81DAFDCF0A2B6503D",
91+
"PublicKey": null,
92+
"Amount": 521,
93+
"Name": "Dipsy",
94+
"Permissions": {
95+
"Base": {
96+
"Perms": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole",
97+
"SetBit": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole"
98+
}
99+
}
100+
},
101+
{
102+
"Address": "1815E3667F406CA3872234D2573014CDE6CD2ABC",
103+
"PublicKey": null,
104+
"Amount": 378,
105+
"Name": "Lala",
106+
"Permissions": {
107+
"Base": {
108+
"Perms": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole",
109+
"SetBit": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole"
110+
}
111+
}
112+
},
113+
{
114+
"Address": "A8174022832E4BA1BB52B380128514F72FB2FEBD",
115+
"PublicKey": null,
116+
"Amount": 191,
117+
"Name": "Po",
118+
"Permissions": {
119+
"Base": {
120+
"Perms": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole",
121+
"SetBit": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole"
122+
}
123+
}
124+
},
125+
{
126+
"Address": "8979C634DFD2F6D20975EBE02C34B5E9C280AB18",
127+
"PublicKey": null,
128+
"Amount": 1304,
129+
"Name": "Tinkie-winkie",
130+
"Permissions": {
131+
"Base": {
132+
"Perms": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole",
133+
"SetBit": "root | send | call | createContract | createAccount | bond | name | proposal | input | batch | identify | hasBase | setBase | unsetBase | setGlobal | hasRole | addRole | removeRole"
134+
}
135+
}
136+
}
137+
],
138+
"Validators": [
139+
{
140+
"Address": "29BB63AD75E2DA3368FC823DC68C01CF1FA87190",
141+
"PublicKey": {
142+
"CurveType": "ed25519",
143+
"PublicKey": "A18679ADC4391630178AC0DB35A115BEAEDA38B3DEABF92AC5FDE31A748DC259"
144+
},
145+
"Amount": 277,
146+
"Name": "Bar",
147+
"UnbondTo": [
148+
{
149+
"Address": "29BB63AD75E2DA3368FC823DC68C01CF1FA87190",
150+
"PublicKey": null,
151+
"Amount": 277
152+
}
153+
]
154+
},
155+
{
156+
"Address": "C92303227C9F0EC569B27B02DB328CFA0A7DF7E0",
157+
"PublicKey": {
158+
"CurveType": "ed25519",
159+
"PublicKey": "E3C56A2C047C9C82036778620E6F9089E5FB38A5D36CE47D9545CBA930C79522"
160+
},
161+
"Amount": 285,
162+
"Name": "Baz",
163+
"UnbondTo": [
164+
{
165+
"Address": "C92303227C9F0EC569B27B02DB328CFA0A7DF7E0",
166+
"PublicKey": null,
167+
"Amount": 285
168+
}
169+
]
170+
},
171+
{
172+
"Address": "900EBED8C6B27F7B606B6CAD34DB03C2C5C0E541",
173+
"PublicKey": {
174+
"CurveType": "ed25519",
175+
"PublicKey": "78133DE07C66C616263FD2D6A54BD3FC0D8CBF3A87BB2E0C410A0C8DEB6189CE"
176+
},
177+
"Amount": 292,
178+
"Name": "Foo",
179+
"UnbondTo": [
180+
{
181+
"Address": "900EBED8C6B27F7B606B6CAD34DB03C2C5C0E541",
182+
"PublicKey": null,
183+
"Amount": 292
184+
}
185+
]
186+
}
187+
]
188+
}`

0 commit comments

Comments
 (0)