diff --git a/pkg/eth/backend.go b/pkg/eth/backend.go index 2f8432e3..b5595778 100644 --- a/pkg/eth/backend.go +++ b/pkg/eth/backend.go @@ -374,21 +374,11 @@ func (b *Backend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Blo return nil, err } - // When num. of uncles = 2, - // Check if calculated uncle hash matches the one in header - // If not, re-order the two uncles - // Assumption: Max num. of uncles in mainnet = 2 - if len(uncles) == 2 { - uncleHash := types.CalcUncleHash(uncles) - if uncleHash != header.UncleHash { - uncles[0], uncles[1] = uncles[1], uncles[0] - - uncleHash = types.CalcUncleHash(uncles) - // Check if uncle hash matches after re-ordering - if uncleHash != header.UncleHash { - log.Error("uncle hash mismatch for block hash: ", hash.Hex()) - } - } + // We should not have any non-determinism in the ordering of the uncles returned to us now + uncleHash := types.CalcUncleHash(uncles) + // Check if uncle hash matches expected hash + if uncleHash != header.UncleHash { + log.Error("uncle hash mismatch for block hash: ", hash.Hex()) } // Fetch transactions @@ -427,15 +417,9 @@ func (b *Backend) GetUnclesByBlockHash(tx *sqlx.Tx, hash common.Hash) ([]*types. return nil, err } - uncles := make([]*types.Header, len(uncleBytes)) - for i, bytes := range uncleBytes { - var uncle types.Header - err = rlp.DecodeBytes(bytes, &uncle) - if err != nil { - return nil, err - } - - uncles[i] = &uncle + uncles := make([]*types.Header, 0) + if err := rlp.DecodeBytes(uncleBytes, uncles); err != nil { + return nil, err } return uncles, nil @@ -448,15 +432,9 @@ func (b *Backend) GetUnclesByBlockHashAndNumber(tx *sqlx.Tx, hash common.Hash, n return nil, err } - uncles := make([]*types.Header, len(uncleBytes)) - for i, bytes := range uncleBytes { - var uncle types.Header - err = rlp.DecodeBytes(bytes, &uncle) - if err != nil { - return nil, err - } - - uncles[i] = &uncle + uncles := make([]*types.Header, 0) + if err := rlp.DecodeBytes(uncleBytes, uncles); err != nil { + return nil, err } return uncles, nil diff --git a/pkg/eth/retriever.go b/pkg/eth/retriever.go index 296f14ca..ee68185e 100644 --- a/pkg/eth/retriever.go +++ b/pkg/eth/retriever.go @@ -365,34 +365,22 @@ func (r *Retriever) RetrieveHeaderByHash(tx *sqlx.Tx, hash common.Hash) (string, return headerResult.CID, headerResult.Data, tx.Get(headerResult, RetrieveHeaderByHashPgStr, hash.Hex()) } -// RetrieveUncles returns the cids and rlp bytes for the uncles corresponding to the provided block hash, number (of non-omner root block) -func (r *Retriever) RetrieveUncles(tx *sqlx.Tx, hash common.Hash, number uint64) ([]string, [][]byte, error) { - uncleResults := make([]ipldResult, 0) - if err := tx.Select(&uncleResults, RetrieveUnclesPgStr, hash.Hex(), number); err != nil { - return nil, nil, err +// RetrieveUncles returns the cid and rlp bytes for the uncle list corresponding to the provided block hash, number (of non-omner root block) +func (r *Retriever) RetrieveUncles(tx *sqlx.Tx, hash common.Hash, number uint64) (string, []byte, error) { + uncleResult := new(ipldResult) + if err := tx.Select(uncleResult, RetrieveUnclesPgStr, hash.Hex(), number); err != nil { + return "", nil, err } - cids := make([]string, len(uncleResults)) - uncles := make([][]byte, len(uncleResults)) - for i, res := range uncleResults { - cids[i] = res.CID - uncles[i] = res.Data - } - return cids, uncles, nil + return uncleResult.CID, uncleResult.Data, nil } -// RetrieveUnclesByBlockHash returns the cids and rlp bytes for the uncles corresponding to the provided block hash (of non-omner root block) -func (r *Retriever) RetrieveUnclesByBlockHash(tx *sqlx.Tx, hash common.Hash) ([]string, [][]byte, error) { - uncleResults := make([]ipldResult, 0) - if err := tx.Select(&uncleResults, RetrieveUnclesByBlockHashPgStr, hash.Hex()); err != nil { - return nil, nil, err +// RetrieveUnclesByBlockHash returns the cid and rlp bytes for the uncle list corresponding to the provided block hash (of non-omner root block) +func (r *Retriever) RetrieveUnclesByBlockHash(tx *sqlx.Tx, hash common.Hash) (string, []byte, error) { + uncleResult := new(ipldResult) + if err := tx.Select(uncleResult, RetrieveUnclesByBlockHashPgStr, hash.Hex()); err != nil { + return "", nil, err } - cids := make([]string, len(uncleResults)) - uncles := make([][]byte, len(uncleResults)) - for i, res := range uncleResults { - cids[i] = res.CID - uncles[i] = res.Data - } - return cids, uncles, nil + return uncleResult.CID, uncleResult.Data, nil } // RetrieveTransactions returns the cids and rlp bytes for the transactions corresponding to the provided block hash, number diff --git a/pkg/eth/sql.go b/pkg/eth/sql.go index 82f3e3d0..8da8cd8a 100644 --- a/pkg/eth/sql.go +++ b/pkg/eth/sql.go @@ -20,7 +20,8 @@ const ( ) WHERE header_cids.block_hash = $1 AND header_cids.block_number = $2 - ORDER BY uncle_cids.parent_hash` + ORDER BY uncle_cids.parent_hash + LIMIT 1` RetrieveUnclesByBlockHashPgStr = `SELECT uncle_cids.cid, data FROM eth.uncle_cids INNER JOIN eth.header_cids ON ( @@ -32,7 +33,8 @@ const ( AND uncle_cids.block_number = blocks.block_number ) WHERE header_cids.block_hash = $1 - ORDER BY uncle_cids.parent_hash` + ORDER BY uncle_cids.parent_hash + LIMIT 1` RetrieveTransactionsPgStr = `SELECT transaction_cids.cid, data FROM eth.transaction_cids INNER JOIN eth.header_cids ON ( diff --git a/pkg/graphql/graphql.go b/pkg/graphql/graphql.go index 715cebb9..da6dd827 100644 --- a/pkg/graphql/graphql.go +++ b/pkg/graphql/graphql.go @@ -36,6 +36,7 @@ import ( "github.com/cerc-io/ipld-eth-server/v4/pkg/eth" "github.com/cerc-io/ipld-eth-server/v4/pkg/shared" + ipld_eth_statedb "github.com/cerc-io/ipld-eth-statedb" ) var (