-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Philip-21 <[email protected]>
- Loading branch information
Showing
5 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package besu | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateGenesis(t *testing.T) { | ||
//test diferent parameter cases for the CreateGenesis() | ||
testCases := []struct { | ||
Name string | ||
addresses []string | ||
blockPeriod int | ||
chainID int64 | ||
}{ | ||
{ | ||
Name: "testcase1", | ||
addresses: []string{"0xAddress1", "0xAddress2"}, | ||
blockPeriod: 10, | ||
chainID: int64(123), | ||
}, | ||
{ | ||
Name: "testcase2", | ||
addresses: []string{"0xAddress3", "0xAddress4"}, | ||
blockPeriod: 7, | ||
chainID: int64(456), | ||
}, | ||
{ | ||
Name: "testcase3", | ||
addresses: []string{"0xAddress14", "0xAddress29"}, | ||
blockPeriod: 22, | ||
chainID: 345, | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
genesis := CreateGenesis(tc.addresses, tc.blockPeriod, tc.chainID) | ||
extraData := "0x0000000000000000000000000000000000000000000000000000000000000000" | ||
alloc := make(map[string]*Alloc) | ||
for _, address := range tc.addresses { | ||
alloc[address] = &Alloc{ | ||
Balance: "0x200000000000000000000000000000000000000000000000000000000000000", | ||
} | ||
extraData += address | ||
} | ||
extraData = strings.ReplaceAll(fmt.Sprintf("%-236s", extraData), " ", "0") | ||
expectedGenesis := &Genesis{ | ||
Config: &GenesisConfig{ | ||
ChainId: tc.chainID, | ||
ConstantinopleFixBlock: 0, | ||
Clique: &CliqueConfig{ | ||
Blockperiodseconds: tc.blockPeriod, | ||
EpochLength: 30000, | ||
}, | ||
}, | ||
Coinbase: "0x0000000000000000000000000000000000000000", | ||
Difficulty: "0x1", | ||
ExtraData: extraData, | ||
GasLimit: "0xffffffff", | ||
MixHash: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
Nonce: "0x0", | ||
Timestamp: "0x5c51a607", | ||
Alloc: alloc, | ||
Number: "0x0", | ||
GasUsed: "0x0", | ||
ParentHash: "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
} | ||
// Assert that the generated Genesis is equal to the expected Genesis | ||
assert.Equal(t, expectedGenesis, genesis, "Generated Genesis does not match the expected Genesis") | ||
}) | ||
} | ||
|
||
} | ||
|
||
func TestWriteGenesisJSON(t *testing.T) { | ||
filepath := "testdata" | ||
|
||
testCases := []struct { | ||
Name string | ||
SampleGenesis Genesis | ||
filename string | ||
}{ | ||
{ | ||
Name: "TestCase1", | ||
SampleGenesis: Genesis{ | ||
Config: &GenesisConfig{ | ||
ChainId: int64(456), | ||
ConstantinopleFixBlock: 0, | ||
Clique: &CliqueConfig{ | ||
Blockperiodseconds: 20, | ||
EpochLength: 2000, | ||
}, | ||
}, | ||
}, | ||
filename: filepath + "/genesis1_output.json", | ||
}, | ||
{ | ||
Name: "TestCase2", | ||
SampleGenesis: Genesis{ | ||
Config: &GenesisConfig{ | ||
ChainId: int64(338), | ||
ConstantinopleFixBlock: 0, | ||
Clique: &CliqueConfig{ | ||
Blockperiodseconds: 40, | ||
EpochLength: 4000, | ||
}, | ||
}, | ||
}, | ||
filename: filepath + "/genesis2_output.json", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
err := tc.SampleGenesis.WriteGenesisJson(tc.filename) | ||
if err != nil { | ||
t.Log("unable to write Genesis JSON", err) | ||
} | ||
// Assert that there is no error | ||
assert.NoError(t, err) | ||
|
||
writtenJSONBytes, err := os.ReadFile(tc.filename) | ||
if err != nil { | ||
t.Log("Unable to write JSON Bytes", err) | ||
} | ||
assert.NoError(t, err) | ||
var writtenGenesis Genesis | ||
|
||
err = json.Unmarshal(writtenJSONBytes, &writtenGenesis) | ||
if err != nil { | ||
t.Log("unable to unmarshal JSON", err) | ||
} | ||
assert.NoError(t, err) | ||
|
||
// Assert that the written Genesis matches the original Genesis | ||
assert.Equal(t, tc.SampleGenesis, writtenGenesis) | ||
}) | ||
|
||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
internal/blockchain/ethereum/besu/testdata/genesis1_output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"config": { | ||
"chainId": 456, | ||
"constantinoplefixblock": 0, | ||
"clique": { | ||
"epochlength": 2000, | ||
"blockperiodseconds": 20 | ||
} | ||
}, | ||
"nonce": "", | ||
"timestamp": "", | ||
"extraData": "", | ||
"gasLimit": "", | ||
"difficulty": "", | ||
"mixHash": "", | ||
"coinbase": "", | ||
"alloc": null, | ||
"number": "", | ||
"gasUsed": "", | ||
"parentHash": "" | ||
} |
21 changes: 21 additions & 0 deletions
21
internal/blockchain/ethereum/besu/testdata/genesis2_output.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"config": { | ||
"chainId": 338, | ||
"constantinoplefixblock": 0, | ||
"clique": { | ||
"epochlength": 4000, | ||
"blockperiodseconds": 40 | ||
} | ||
}, | ||
"nonce": "", | ||
"timestamp": "", | ||
"extraData": "", | ||
"gasLimit": "", | ||
"difficulty": "", | ||
"mixHash": "", | ||
"coinbase": "", | ||
"alloc": null, | ||
"number": "", | ||
"gasUsed": "", | ||
"parentHash": "" | ||
} |