diff --git a/core/types/account.go b/core/types/account.go deleted file mode 100644 index efc0927770..0000000000 --- a/core/types/account.go +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2024 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package types - -import ( - "bytes" - "encoding/hex" - "encoding/json" - "fmt" - "math/big" - - "github.com/ava-labs/libevm/common" - "github.com/ava-labs/libevm/common/hexutil" - "github.com/ava-labs/libevm/common/math" -) - -//go:generate go run github.com/fjl/gencodec -type Account -field-override accountMarshaling -out gen_account.go - -// Account represents an Ethereum account and its attached data. -// This type is used to specify accounts in the genesis block state, and -// is also useful for JSON encoding/decoding of accounts. -type Account struct { - Code []byte `json:"code,omitempty"` - Storage map[common.Hash]common.Hash `json:"storage,omitempty"` - Balance *big.Int `json:"balance" gencodec:"required"` - Nonce uint64 `json:"nonce,omitempty"` - - // used in tests - PrivateKey []byte `json:"secretKey,omitempty"` -} - -type accountMarshaling struct { - Code hexutil.Bytes - Balance *math.HexOrDecimal256 - Nonce math.HexOrDecimal64 - Storage map[storageJSON]storageJSON - PrivateKey hexutil.Bytes -} - -// storageJSON represents a 256 bit byte array, but allows less than 256 bits when -// unmarshaling from hex. -type storageJSON common.Hash - -func (h *storageJSON) UnmarshalText(text []byte) error { - text = bytes.TrimPrefix(text, []byte("0x")) - if len(text) > 64 { - return fmt.Errorf("too many hex characters in storage key/value %q", text) - } - offset := len(h) - len(text)/2 // pad on the left - if _, err := hex.Decode(h[offset:], text); err != nil { - return fmt.Errorf("invalid hex storage key/value %q", text) - } - return nil -} - -func (h storageJSON) MarshalText() ([]byte, error) { - return hexutil.Bytes(h[:]).MarshalText() -} - -// GenesisAlloc specifies the initial state of a genesis block. -type GenesisAlloc map[common.Address]Account - -func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error { - m := make(map[common.UnprefixedAddress]Account) - if err := json.Unmarshal(data, &m); err != nil { - return err - } - *ga = make(GenesisAlloc) - for addr, a := range m { - (*ga)[common.Address(addr)] = a - } - return nil -} diff --git a/core/types/gen_account.go b/core/types/gen_account.go deleted file mode 100644 index c3c7fb3fdf..0000000000 --- a/core/types/gen_account.go +++ /dev/null @@ -1,73 +0,0 @@ -// Code generated by github.com/fjl/gencodec. DO NOT EDIT. - -package types - -import ( - "encoding/json" - "errors" - "math/big" - - "github.com/ava-labs/libevm/common" - "github.com/ava-labs/libevm/common/hexutil" - "github.com/ava-labs/libevm/common/math" -) - -var _ = (*accountMarshaling)(nil) - -// MarshalJSON marshals as JSON. -func (a Account) MarshalJSON() ([]byte, error) { - type Account struct { - Code hexutil.Bytes `json:"code,omitempty"` - Storage map[storageJSON]storageJSON `json:"storage,omitempty"` - Balance *math.HexOrDecimal256 `json:"balance" gencodec:"required"` - Nonce math.HexOrDecimal64 `json:"nonce,omitempty"` - PrivateKey hexutil.Bytes `json:"secretKey,omitempty"` - } - var enc Account - enc.Code = a.Code - if a.Storage != nil { - enc.Storage = make(map[storageJSON]storageJSON, len(a.Storage)) - for k, v := range a.Storage { - enc.Storage[storageJSON(k)] = storageJSON(v) - } - } - enc.Balance = (*math.HexOrDecimal256)(a.Balance) - enc.Nonce = math.HexOrDecimal64(a.Nonce) - enc.PrivateKey = a.PrivateKey - return json.Marshal(&enc) -} - -// UnmarshalJSON unmarshals from JSON. -func (a *Account) UnmarshalJSON(input []byte) error { - type Account struct { - Code *hexutil.Bytes `json:"code,omitempty"` - Storage map[storageJSON]storageJSON `json:"storage,omitempty"` - Balance *math.HexOrDecimal256 `json:"balance" gencodec:"required"` - Nonce *math.HexOrDecimal64 `json:"nonce,omitempty"` - PrivateKey *hexutil.Bytes `json:"secretKey,omitempty"` - } - var dec Account - if err := json.Unmarshal(input, &dec); err != nil { - return err - } - if dec.Code != nil { - a.Code = *dec.Code - } - if dec.Storage != nil { - a.Storage = make(map[common.Hash]common.Hash, len(dec.Storage)) - for k, v := range dec.Storage { - a.Storage[common.Hash(k)] = common.Hash(v) - } - } - if dec.Balance == nil { - return errors.New("missing required field 'balance' for Account") - } - a.Balance = (*big.Int)(dec.Balance) - if dec.Nonce != nil { - a.Nonce = uint64(*dec.Nonce) - } - if dec.PrivateKey != nil { - a.PrivateKey = *dec.PrivateKey - } - return nil -} diff --git a/core/types/hashes.go b/core/types/hashes.go deleted file mode 100644 index 2a5ed05aa5..0000000000 --- a/core/types/hashes.go +++ /dev/null @@ -1,66 +0,0 @@ -// (c) 2023, Ava Labs, Inc. -// -// This file is a derived work, based on the go-ethereum library whose original -// notices appear below. -// -// It is distributed under a license compatible with the licensing terms of the -// original code from which it is derived. -// -// Much love to the original authors for their work. -// ********** -// Copyright 2023 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package types - -import ( - "github.com/ava-labs/libevm/common" - "github.com/ava-labs/libevm/crypto" - "github.com/ava-labs/libevm/log" -) - -var ( - // EmptyRootHash is the known root hash of an empty merkle trie. - EmptyRootHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") - - // EmptyUncleHash is the known hash of the empty uncle set. - EmptyUncleHash = rlpHash([]*Header(nil)) // 1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347 - - // EmptyCodeHash is the known hash of the empty EVM bytecode. - EmptyCodeHash = crypto.Keccak256Hash(nil) // c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470 - - // EmptyTxsHash is the known hash of the empty transaction set. - EmptyTxsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") - - // EmptyReceiptsHash is the known hash of the empty receipt set. - EmptyReceiptsHash = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") - - // EmptyVerkleHash is the known hash of an empty verkle trie. - EmptyVerkleHash = common.Hash{} - - // EmptyExtDataHash is the known hash of empty extdata bytes. - EmptyExtDataHash = rlpHash([]byte(nil)) -) - -// TrieRootHash returns the hash itself if it's non-empty or the predefined -// emptyHash one instead. -func TrieRootHash(hash common.Hash) common.Hash { - if hash == (common.Hash{}) { - log.Error("Zero trie root hash!") - return EmptyRootHash - } - return hash -} diff --git a/core/types/hashes_ext.go b/core/types/hashes_ext.go new file mode 100644 index 0000000000..2ea9283370 --- /dev/null +++ b/core/types/hashes_ext.go @@ -0,0 +1,7 @@ +// (c) 2025, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package types + +// EmptyExtDataHash is the known hash of empty extdata bytes. +var EmptyExtDataHash = rlpHash([]byte(nil)) diff --git a/core/types/hashing.go b/core/types/hashing.go index b6b631c7bc..ece395d928 100644 --- a/core/types/hashing.go +++ b/core/types/hashing.go @@ -27,7 +27,6 @@ package types import ( - "bytes" "sync" "github.com/ava-labs/libevm/common" @@ -41,11 +40,6 @@ var hasherPool = sync.Pool{ New: func() interface{} { return sha3.NewLegacyKeccak256() }, } -// encodeBufferPool holds temporary encoder buffers for DeriveSha and TX encoding. -var encodeBufferPool = sync.Pool{ - New: func() interface{} { return new(bytes.Buffer) }, -} - // rlpHash encodes x and hashes the encoded bytes. func rlpHash(x interface{}) (h common.Hash) { sha := hasherPool.Get().(crypto.KeccakState) @@ -55,60 +49,3 @@ func rlpHash(x interface{}) (h common.Hash) { sha.Read(h[:]) return h } - -// TrieHasher is the tool used to calculate the hash of derivable list. -// This is internal, do not use. -type TrieHasher interface { - Reset() - Update([]byte, []byte) error - Hash() common.Hash -} - -// DerivableList is the input to DeriveSha. -// It is implemented by the 'Transactions' and 'Receipts' types. -// This is internal, do not use these methods. -type DerivableList interface { - Len() int - EncodeIndex(int, *bytes.Buffer) -} - -func encodeForDerive(list DerivableList, i int, buf *bytes.Buffer) []byte { - buf.Reset() - list.EncodeIndex(i, buf) - // It's really unfortunate that we need to perform this copy. - // StackTrie holds onto the values until Hash is called, so the values - // written to it must not alias. - return common.CopyBytes(buf.Bytes()) -} - -// DeriveSha creates the tree hashes of transactions, receipts, and withdrawals in a block header. -func DeriveSha(list DerivableList, hasher TrieHasher) common.Hash { - hasher.Reset() - - valueBuf := encodeBufferPool.Get().(*bytes.Buffer) - defer encodeBufferPool.Put(valueBuf) - - // StackTrie requires values to be inserted in increasing hash order, which is not the - // order that `list` provides hashes in. This insertion sequence ensures that the - // order is correct. - // - // The error returned by hasher is omitted because hasher will produce an incorrect - // hash in case any error occurs. - var indexBuf []byte - for i := 1; i < list.Len() && i <= 0x7f; i++ { - indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(i)) - value := encodeForDerive(list, i, valueBuf) - hasher.Update(indexBuf, value) - } - if list.Len() > 0 { - indexBuf = rlp.AppendUint64(indexBuf[:0], 0) - value := encodeForDerive(list, 0, valueBuf) - hasher.Update(indexBuf, value) - } - for i := 0x80; i < list.Len(); i++ { - indexBuf = rlp.AppendUint64(indexBuf[:0], uint64(i)) - value := encodeForDerive(list, i, valueBuf) - hasher.Update(indexBuf, value) - } - return hasher.Hash() -} diff --git a/core/types/imports.go b/core/types/imports.go index 2a0ad279bf..9af78fe8ea 100644 --- a/core/types/imports.go +++ b/core/types/imports.go @@ -8,11 +8,11 @@ import ( ) // The following types are used directly as their upstream definitions. -// So we list them all here to avoid having many individual files. type ( AccessList = ethtypes.AccessList AccessListTx = ethtypes.AccessListTx AccessTuple = ethtypes.AccessTuple + Account = ethtypes.Account BlobTx = ethtypes.BlobTx BlobTxSidecar = ethtypes.BlobTxSidecar Block = ethtypes.Block @@ -20,10 +20,13 @@ type ( Blocks = ethtypes.Blocks Bloom = ethtypes.Bloom Body = ethtypes.Body + DerivableList = ethtypes.DerivableList DynamicFeeTx = ethtypes.DynamicFeeTx + GenesisAlloc = ethtypes.GenesisAlloc Header = ethtypes.Header HomesteadSigner = ethtypes.HomesteadSigner LegacyTx = ethtypes.LegacyTx + Log = ethtypes.Log Receipt = ethtypes.Receipt ReceiptForStorage = ethtypes.ReceiptForStorage Receipts = ethtypes.Receipts @@ -32,6 +35,7 @@ type ( StateAccount = ethtypes.StateAccount Transaction = ethtypes.Transaction Transactions = ethtypes.Transactions + TrieHasher = ethtypes.TrieHasher TxByNonce = ethtypes.TxByNonce TxData = ethtypes.TxData ) @@ -50,6 +54,16 @@ const ( LegacyTxType = ethtypes.LegacyTxType ) +// The following variables are used directly as their upstream definitions. +var ( + EmptyRootHash = ethtypes.EmptyRootHash + EmptyUncleHash = ethtypes.EmptyUncleHash + EmptyCodeHash = ethtypes.EmptyCodeHash + EmptyTxsHash = ethtypes.EmptyTxsHash + EmptyReceiptsHash = ethtypes.EmptyReceiptsHash + EmptyVerkleHash = ethtypes.EmptyVerkleHash +) + // The following functions are used directly as their upstream definitions. var ( BloomLookup = ethtypes.BloomLookup @@ -57,6 +71,7 @@ var ( CalcUncleHash = ethtypes.CalcUncleHash CopyHeader = ethtypes.CopyHeader CreateBloom = ethtypes.CreateBloom + DeriveSha = ethtypes.DeriveSha EncodeNonce = ethtypes.EncodeNonce FullAccount = ethtypes.FullAccount FullAccountRLP = ethtypes.FullAccountRLP @@ -67,6 +82,7 @@ var ( NewReceipt = ethtypes.NewReceipt NewTransaction = ethtypes.NewTransaction SlimAccountRLP = ethtypes.SlimAccountRLP + TrieRootHash = ethtypes.TrieRootHash // Signers LatestSigner = ethtypes.LatestSigner diff --git a/core/types/log.go b/core/types/log.go deleted file mode 100644 index 5ddb090034..0000000000 --- a/core/types/log.go +++ /dev/null @@ -1,42 +0,0 @@ -// (c) 2019-2020, Ava Labs, Inc. -// -// This file is a derived work, based on the go-ethereum library whose original -// notices appear below. -// -// It is distributed under a license compatible with the licensing terms of the -// original code from which it is derived. -// -// Much love to the original authors for their work. -// ********** -// Copyright 2014 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package types - -import ethtypes "github.com/ava-labs/libevm/core/types" - -// Log represents a contract log event. These events are generated by the LOG opcode and -// stored/indexed by the node. -type Log = ethtypes.Log - -// FlattenLogs converts a nested array of logs to a single array of logs. -func FlattenLogs(list [][]*Log) []*Log { - var flat []*Log - for _, logs := range list { - flat = append(flat, logs...) - } - return flat -} diff --git a/core/types/log_ext.go b/core/types/log_ext.go new file mode 100644 index 0000000000..ca0958257b --- /dev/null +++ b/core/types/log_ext.go @@ -0,0 +1,12 @@ +// (c) 2019-2025, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. +package types + +// FlattenLogs converts a nested array of logs to a single array of logs. +func FlattenLogs(list [][]*Log) []*Log { + var flat []*Log + for _, logs := range list { + flat = append(flat, logs...) + } + return flat +} diff --git a/core/types/state_account.go b/core/types/state_account.go deleted file mode 100644 index fe080c144b..0000000000 --- a/core/types/state_account.go +++ /dev/null @@ -1,39 +0,0 @@ -// (c) 2019-2025, Ava Labs, Inc. -// -// This file is a derived work, based on the go-ethereum library whose original -// notices appear below. -// -// It is distributed under a license compatible with the licensing terms of the -// original code from which it is derived. -// -// Much love to the original authors for their work. -// ********** -// Copyright 2021 The go-ethereum Authors -// This file is part of the go-ethereum library. -// -// The go-ethereum library is free software: you can redistribute it and/or modify -// it under the terms of the GNU Lesser General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// The go-ethereum library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Lesser General Public License for more details. -// -// You should have received a copy of the GNU Lesser General Public License -// along with the go-ethereum library. If not, see . - -package types - -import ( - ethtypes "github.com/ava-labs/libevm/core/types" -) - -type isMultiCoin bool - -var IsMultiCoinPayloads = extras.StateAccount - -func IsMultiCoin(s ethtypes.StateOrSlimAccount) bool { - return bool(IsMultiCoinPayloads.Get(s)) -} diff --git a/core/types/state_account_ext.go b/core/types/state_account_ext.go new file mode 100644 index 0000000000..d8662382dd --- /dev/null +++ b/core/types/state_account_ext.go @@ -0,0 +1,16 @@ +// (c) 2025, Ava Labs, Inc. All rights reserved. +// See the file LICENSE for licensing terms. + +package types + +import ( + ethtypes "github.com/ava-labs/libevm/core/types" +) + +type isMultiCoin bool + +var IsMultiCoinPayloads = extras.StateAccount + +func IsMultiCoin(s ethtypes.StateOrSlimAccount) bool { + return bool(IsMultiCoinPayloads.Get(s)) +} diff --git a/go.mod b/go.mod index 93d6742139..2dcd3bf8e2 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/crate-crypto/go-ipa v0.0.0-20231025140028-3c0104f4b233 github.com/davecgh/go-spew v1.1.1 github.com/deckarep/golang-set/v2 v2.1.0 - github.com/fjl/gencodec v0.1.1-0.20250303202438-8effaadd1b1b + github.com/fjl/gencodec v0.1.1-0.20250314100332-a3c3302847ce github.com/google/go-cmp v0.7.0 github.com/gorilla/rpc v1.2.0 github.com/gorilla/websocket v1.5.0 diff --git a/go.sum b/go.sum index 6173d7978a..9960f4807a 100644 --- a/go.sum +++ b/go.sum @@ -173,8 +173,8 @@ github.com/ethereum/c-kzg-4844 v0.4.0 h1:3MS1s4JtA868KpJxroZoepdV0ZKBp3u/O5HcZ7R github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= github.com/fasthttp-contrib/websocket v0.0.0-20160511215533-1f3b11f56072/go.mod h1:duJ4Jxv5lDcvg4QuQr0oowTf7dz4/CR8NtyCooz9HL8= github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M= -github.com/fjl/gencodec v0.1.1-0.20250303202438-8effaadd1b1b h1:TUhCXGKs8GbAAFj/OrttBLbBUwd89ES1H0DDvWUHmf4= -github.com/fjl/gencodec v0.1.1-0.20250303202438-8effaadd1b1b/go.mod h1:chDHL3wKXuBgauP8x3XNZkl5EIAR5SoCTmmmDTZRzmw= +github.com/fjl/gencodec v0.1.1-0.20250314100332-a3c3302847ce h1:MjcqyNZgJADSmUq8jNoDNHm+zzJpxTQSB5zmdeowI98= +github.com/fjl/gencodec v0.1.1-0.20250314100332-a3c3302847ce/go.mod h1:chDHL3wKXuBgauP8x3XNZkl5EIAR5SoCTmmmDTZRzmw= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=