Skip to content

Commit

Permalink
ethereum besu unit test created
Browse files Browse the repository at this point in the history
Signed-off-by: Philip-21 <[email protected]>
  • Loading branch information
Philip-21 committed Dec 15, 2023
1 parent 7dae6c2 commit 15cba86
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 1 deletion.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ require (
github.com/otiai10/copy v1.7.0
github.com/spf13/cobra v1.5.0
github.com/spf13/viper v1.12.1-0.20220712161005-5247643f0235
github.com/stretchr/testify v1.8.0
github.com/stretchr/objx v0.5.1 // indirect
github.com/stretchr/testify v1.8.4
golang.org/x/crypto v0.10.0
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,9 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -876,6 +879,9 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.3.0/go.mod h1:YzJjq/33h7nrwdY+iHMhEOEEbW0ovIz0tB6t6PwAXzs=
github.com/subosito/gotenv v1.4.0 h1:yAzM1+SmVcz5R4tXGsNMu1jUl2aOJXoiWUCEwwnGrvs=
Expand Down
145 changes: 145 additions & 0 deletions internal/blockchain/ethereum/besu/genesis_test.go
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 internal/blockchain/ethereum/besu/testdata/genesis1_output.json
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 internal/blockchain/ethereum/besu/testdata/genesis2_output.json
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": ""
}

0 comments on commit 15cba86

Please sign in to comment.