diff --git a/database.go b/database.go deleted file mode 100644 index 35e6ace..0000000 --- a/database.go +++ /dev/null @@ -1,28 +0,0 @@ -package ipld_eth_statedb - -var _ Database = &DB{} - -// NewPostgresDB returns a postgres.DB using the provided driver -func NewPostgresDB(driver Driver) *DB { - return &DB{driver} -} - -// DB implements sql.Database using a configured driver and Postgres statement syntax -type DB struct { - Driver -} - -// GetContractCodeStmt satisfies the Statements interface -func (db *DB) GetContractCodeStmt() string { - return GetContractCodePgStr -} - -// GetStateAccountStmt satisfies the Statements interface -func (db *DB) GetStateAccountStmt() string { - return GetStateAccount -} - -// GetStorageSlotStmt satisfies the Statements interface -func (db *DB) GetStorageSlotStmt() string { - return GetStorageSlot -} diff --git a/access_list.go b/direct_by_leaf/access_list.go similarity index 99% rename from access_list.go rename to direct_by_leaf/access_list.go index 7aa2579..7e17a55 100644 --- a/access_list.go +++ b/direct_by_leaf/access_list.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package state import ( "github.com/ethereum/go-ethereum/common" diff --git a/journal.go b/direct_by_leaf/journal.go similarity index 99% rename from journal.go rename to direct_by_leaf/journal.go index 8e0c655..0ea1fb5 100644 --- a/journal.go +++ b/direct_by_leaf/journal.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package state import ( "math/big" diff --git a/sql.go b/direct_by_leaf/sql.go similarity index 98% rename from sql.go rename to direct_by_leaf/sql.go index 09b2bc8..0949cb4 100644 --- a/sql.go +++ b/direct_by_leaf/sql.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package state const ( GetContractCodePgStr = `SELECT data FROM ipld.blocks WHERE key = $1` diff --git a/state_database.go b/direct_by_leaf/state_database.go similarity index 96% rename from state_database.go rename to direct_by_leaf/state_database.go index cb2c7b4..65204bc 100644 --- a/state_database.go +++ b/direct_by_leaf/state_database.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package state import ( "context" @@ -14,6 +14,7 @@ import ( "github.com/ethereum/go-ethereum/statediff/indexer/ipld" util "github.com/cerc-io/ipld-eth-statedb/internal" + "github.com/cerc-io/ipld-eth-statedb/sql" ) const ( @@ -41,13 +42,13 @@ type StateDatabase interface { var _ StateDatabase = &stateDatabase{} type stateDatabase struct { - db Database + db sql.Database codeSizeCache *lru.Cache codeCache *fastcache.Cache } // NewStateDatabase returns a new Database implementation using the passed parameters -func NewStateDatabase(db Database) *stateDatabase { +func NewStateDatabase(db sql.Database) *stateDatabase { csc, _ := lru.New(codeSizeCacheSize) return &stateDatabase{ db: db, diff --git a/state_object.go b/direct_by_leaf/state_object.go similarity index 99% rename from state_object.go rename to direct_by_leaf/state_object.go index b18c5bc..d388589 100644 --- a/state_object.go +++ b/direct_by_leaf/state_object.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package state import ( "bytes" diff --git a/statedb.go b/direct_by_leaf/statedb.go similarity index 99% rename from statedb.go rename to direct_by_leaf/statedb.go index 8933178..ff2c0fd 100644 --- a/statedb.go +++ b/direct_by_leaf/statedb.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package state import ( "fmt" diff --git a/statedb_test.go b/direct_by_leaf/statedb_test.go similarity index 93% rename from statedb_test.go rename to direct_by_leaf/statedb_test.go index e26dd4f..ad46f0d 100644 --- a/statedb_test.go +++ b/direct_by_leaf/statedb_test.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb_test +package state_test import ( "context" @@ -16,8 +16,9 @@ import ( "github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres" "github.com/ethereum/go-ethereum/statediff/indexer/ipld" - statedb "github.com/cerc-io/ipld-eth-statedb" + state "github.com/cerc-io/ipld-eth-statedb/direct_by_leaf" util "github.com/cerc-io/ipld-eth-statedb/internal" + "github.com/cerc-io/ipld-eth-statedb/sql" ) var ( @@ -52,7 +53,7 @@ var ( AccountPK, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a") AccountAddress = crypto.PubkeyToAddress(AccountPK.PublicKey) //0x703c4b2bD70c169f5717101CaeE543299Fc946C7 - AccountLeafKey = crypto.Keccak256Hash(AccountAddress.Bytes()) + AccountLeafKey = crypto.Keccak256Hash(AccountAddress[:]) AccountCode = []byte{0, 1, 2, 3, 4, 5, 6, 7} AccountCodeHash = crypto.Keccak256Hash(AccountCode) @@ -73,7 +74,7 @@ var ( accountRLP, _ = rlp.EncodeToBytes(&Account) accountAndLeafRLP, _ = rlp.EncodeToBytes(&[]interface{}{AccountLeafKey, accountRLP}) AccountCID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, accountAndLeafRLP, multihash.KECCAK_256) - AccountCodeCID, _ = util.Keccak256ToCid(ipld.RawBinary, AccountCodeHash.Bytes()) + AccountCodeCID, _ = util.Keccak256ToCid(ipld.RawBinary, AccountCodeHash[:]) StoredValueRLP, _ = rlp.EncodeToBytes(StoredValue) StoredValueRLP2, _ = rlp.EncodeToBytes("something") @@ -108,11 +109,10 @@ func TestPGXSuite(t *testing.T) { require.NoError(t, tx.Commit(testCtx)) }) - driver := statedb.NewPGXDriverFromPool(context.Background(), pool) - database := statedb.NewPostgresDB(driver) + database := sql.NewPGXDriverFromPool(context.Background(), pool) insertSuiteData(t, database) - db := statedb.NewStateDatabase(database) + db := state.NewStateDatabase(database) require.NoError(t, err) testSuite(t, db) } @@ -140,16 +140,15 @@ func TestSQLXSuite(t *testing.T) { require.NoError(t, tx.Commit()) }) - driver := statedb.NewSQLXDriverFromPool(context.Background(), pool) - database := statedb.NewPostgresDB(driver) + database := sql.NewSQLXDriverFromPool(context.Background(), pool) insertSuiteData(t, database) - db := statedb.NewStateDatabase(database) + db := state.NewStateDatabase(database) require.NoError(t, err) testSuite(t, db) } -func insertSuiteData(t *testing.T, database statedb.Database) { +func insertSuiteData(t *testing.T, database sql.Database) { require.NoError(t, insertHeaderCID(database, BlockHash.String(), BlockParentHash.String(), BlockNumber.Uint64())) require.NoError(t, insertHeaderCID(database, BlockHash2.String(), BlockHash.String(), BlockNumber2)) require.NoError(t, insertHeaderCID(database, BlockHash3.String(), BlockHash2.String(), BlockNumber3)) @@ -233,7 +232,7 @@ func insertSuiteData(t *testing.T, database statedb.Database) { require.NoError(t, insertContractCode(database)) } -func testSuite(t *testing.T, db statedb.StateDatabase) { +func testSuite(t *testing.T, db state.StateDatabase) { t.Run("Database", func(t *testing.T) { size, err := db.ContractCodeSize(AccountCodeHash) require.NoError(t, err) @@ -296,7 +295,7 @@ func testSuite(t *testing.T, db statedb.StateDatabase) { }) t.Run("StateDB", func(t *testing.T) { - sdb, err := statedb.New(BlockHash, db) + sdb, err := state.New(BlockHash, db) require.NoError(t, err) checkAccountUnchanged := func() { @@ -344,7 +343,7 @@ func testSuite(t *testing.T, db statedb.StateDatabase) { }) } -func insertHeaderCID(db statedb.Database, blockHash, parentHash string, blockNumber uint64) error { +func insertHeaderCID(db sql.Database, blockHash, parentHash string, blockNumber uint64) error { cid, err := util.Keccak256ToCid(ipld.MEthHeader, common.HexToHash(blockHash).Bytes()) if err != nil { return err @@ -395,7 +394,7 @@ type stateModel struct { Removed bool } -func insertStateCID(db statedb.Database, cidModel stateModel) error { +func insertStateCID(db sql.Database, cidModel stateModel) error { sql := `INSERT INTO eth.state_cids ( block_number, header_id, @@ -434,7 +433,7 @@ type storageModel struct { Removed bool } -func insertStorageCID(db statedb.Database, cidModel storageModel) error { +func insertStorageCID(db sql.Database, cidModel storageModel) error { sql := `INSERT INTO eth.storage_cids ( block_number, header_id, @@ -458,7 +457,7 @@ func insertStorageCID(db statedb.Database, cidModel storageModel) error { return err } -func insertContractCode(db statedb.Database) error { +func insertContractCode(db sql.Database) error { sql := `INSERT INTO ipld.blocks (block_number, key, data) VALUES ($1, $2, $3)` _, err := db.Exec(testCtx, sql, BlockNumber.Uint64(), AccountCodeCID.String(), AccountCode) return err diff --git a/transient_storage.go b/direct_by_leaf/transient_storage.go similarity index 98% rename from transient_storage.go rename to direct_by_leaf/transient_storage.go index 91deec5..66e563e 100644 --- a/transient_storage.go +++ b/direct_by_leaf/transient_storage.go @@ -14,7 +14,7 @@ // You should have received a copy of the GNU Lesser General Public License // along with the go-ethereum library. If not, see . -package ipld_eth_statedb +package state import ( "github.com/ethereum/go-ethereum/common" diff --git a/interfaces.go b/sql/interfaces.go similarity index 70% rename from interfaces.go rename to sql/interfaces.go index 66b0a98..448a647 100644 --- a/interfaces.go +++ b/sql/interfaces.go @@ -1,14 +1,11 @@ -package ipld_eth_statedb +package sql import ( "context" ) // Database interfaces to support multiple Postgres drivers -type Database interface { - Driver - Statements -} +type Database = Driver // Driver interface has all the methods required by a driver implementation to support the sql indexer type Driver interface { @@ -25,10 +22,3 @@ type ScannableRow interface { type Result interface { RowsAffected() (int64, error) } - -// Statements interface to accommodate different SQL query syntax -type Statements interface { - GetContractCodeStmt() string - GetStateAccountStmt() string - GetStorageSlotStmt() string -} diff --git a/pgx.go b/sql/pgx.go similarity index 97% rename from pgx.go rename to sql/pgx.go index e60a6d5..1ba3cc9 100644 --- a/pgx.go +++ b/sql/pgx.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package sql import ( "context" diff --git a/sqlx.go b/sql/sqlx.go similarity index 96% rename from sqlx.go rename to sql/sqlx.go index 2eed275..700d8c2 100644 --- a/sqlx.go +++ b/sql/sqlx.go @@ -1,4 +1,4 @@ -package ipld_eth_statedb +package sql import ( "context"