Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(smt): block info root and test #96

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions smt/pkg/blockinfo/block_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,48 @@ import (
)

func BuildBlockInfoTree(
smt *smt.SMT,
blockHash *big.Int,
coinbase *big.Int,
blockNumber *big.Int,
gasLimit *big.Int,
timestamp *big.Int,
ger *big.Int,
blockHashL1 *big.Int) (*big.Int, error) {

_, err := setBlockHash(smt, blockHash)
if err != nil {
return nil, err
}
_, err = setCoinbase(smt, coinbase)
if err != nil {
return nil, err
}
_, err = setBlockNumber(smt, blockNumber)
if err != nil {
return nil, err
}
_, err = setGasLimit(smt, gasLimit)
if err != nil {
return nil, err
}
_, err = setTimestamp(smt, timestamp)
if err != nil {
return nil, err
}
_, err = setGer(smt, ger)
if err != nil {
return nil, err
}
_, err = setBlockHashL1(smt, blockHashL1)
if err != nil {
return nil, err
}

return smt.LastRoot(), nil
}

func BuildReceiptTree(
smt *smt.SMT,
txIndex *big.Int,
logs []*ethTypes.Log,
Expand Down Expand Up @@ -128,3 +170,94 @@ func setTxLog(smt *smt.SMT, txIndex *big.Int, logIndex *big.Int, log *big.Int) (

return resp.NewRootScalar.ToBigInt(), nil
}

func setBlockHash(smt *smt.SMT, blockHash *big.Int) (*big.Int, error) {
key, err := KeyBlockHeaderParams(big.NewInt(IndexBlockHeaderParamBlockHash))
if err != nil {
return nil, err
}
resp, err := smt.InsertKA(key, blockHash)
if err != nil {
return nil, err
}

return resp.NewRootScalar.ToBigInt(), nil
}

func setCoinbase(smt *smt.SMT, coinbase *big.Int) (*big.Int, error) {
key, err := KeyBlockHeaderParams(big.NewInt(IndexBlockHeaderParamCoinbase))
if err != nil {
return nil, err
}
resp, err := smt.InsertKA(key, coinbase)
if err != nil {
return nil, err
}

return resp.NewRootScalar.ToBigInt(), nil
}

func setBlockNumber(smt *smt.SMT, blockNumber *big.Int) (*big.Int, error) {
key, err := KeyBlockHeaderParams(big.NewInt(IndexBlockHeaderParamNumber))
if err != nil {
return nil, err
}
resp, err := smt.InsertKA(key, blockNumber)
if err != nil {
return nil, err
}

return resp.NewRootScalar.ToBigInt(), nil
}

func setGasLimit(smt *smt.SMT, gasLimit *big.Int) (*big.Int, error) {
key, err := KeyBlockHeaderParams(big.NewInt(IndexBlockHeaderParamGasLimit))
if err != nil {
return nil, err
}
resp, err := smt.InsertKA(key, gasLimit)
if err != nil {
return nil, err
}

return resp.NewRootScalar.ToBigInt(), nil
}

func setTimestamp(smt *smt.SMT, timestamp *big.Int) (*big.Int, error) {
key, err := KeyBlockHeaderParams(big.NewInt(IndexBlockHeaderParamTimestamp))
if err != nil {
return nil, err
}
resp, err := smt.InsertKA(key, timestamp)
if err != nil {
return nil, err
}

return resp.NewRootScalar.ToBigInt(), nil
}

func setGer(smt *smt.SMT, ger *big.Int) (*big.Int, error) {
key, err := KeyBlockHeaderParams(big.NewInt(IndexBlockHeaderParamGer))
if err != nil {
return nil, err
}
resp, err := smt.InsertKA(key, ger)
if err != nil {
return nil, err
}

return resp.NewRootScalar.ToBigInt(), nil
}

func setBlockHashL1(smt *smt.SMT, blockHashL1 *big.Int) (*big.Int, error) {
key, err := KeyBlockHeaderParams(big.NewInt(IndexBlockHeaderParamBlockHashL1))
if err != nil {
return nil, err
}
resp, err := smt.InsertKA(key, blockHashL1)
if err != nil {
return nil, err
}

return resp.NewRootScalar.ToBigInt(), nil
}
28 changes: 26 additions & 2 deletions smt/pkg/blockinfo/block_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,33 @@ import (
"github.com/ledgerwatch/erigon/smt/pkg/smt"
"math/big"
"testing"
"github.com/stretchr/testify/require"
)

func TestBlockInfo(t *testing.T) {
func TestBlockInfoTree(t *testing.T) {
smt := smt.NewSMT(nil)

sequencerAddress := new(big.Int)
sequencerAddress.SetString("a9127a157cee3cd2452a194e4efc2f8a5612cfc36c66e768700727ede4d0e2e6", 16)
newBlockNumber := big.NewInt(1)
blockGasLimit := big.NewInt(1)
finalTimestamp := big.NewInt(1)
finalGER := big.NewInt(1)
finalBlockHash := new(big.Int)
finalBlockHash.SetString("a9127a157cee3cd2452a194e4efc2f8a5612cfc36c66e768700727ede4d0e2e6", 16)
oldBlockHash := new(big.Int)
oldBlockHash.SetString("a9127a157cee3cd2452a194e4efc2f8a5612cfc36c66e768700727ede4d0e2e6", 16)

root, err := BuildBlockInfoTree(smt, oldBlockHash, sequencerAddress, newBlockNumber, blockGasLimit, finalTimestamp, finalGER, finalBlockHash)
require.NoError(t, err, "Building block info tree should not produce an error")

expectedRoot := new(big.Int)
expectedRoot.SetString("208579169e61f707c35ab2e4e6c37179eb016ff39e6ab5d1f1389ca85d135d58", 16)

require.Equal(t, expectedRoot, root, "The calculated root hash should match the expected root hash")
}

func TestReceiptTree(t *testing.T) {
smt := smt.NewSMT(nil)

logs := []*types.Log{
Expand Down Expand Up @@ -38,7 +62,7 @@ func TestBlockInfo(t *testing.T) {
},
}

root, err := BuildBlockInfoTree(
root, err := BuildReceiptTree(
smt,
big.NewInt(1),
logs,
Expand Down