Reorganize packages
* direct_by_leaf/ is original StateDB package * sql/ for SQL DB interfaces
This commit is contained in:
parent
5790ff0439
commit
3625e22949
28
database.go
28
database.go
@ -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
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/big"
|
"math/big"
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package state
|
||||||
|
|
||||||
const (
|
const (
|
||||||
GetContractCodePgStr = `SELECT data FROM ipld.blocks WHERE key = $1`
|
GetContractCodePgStr = `SELECT data FROM ipld.blocks WHERE key = $1`
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -14,6 +14,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/statediff/indexer/ipld"
|
"github.com/ethereum/go-ethereum/statediff/indexer/ipld"
|
||||||
|
|
||||||
util "github.com/cerc-io/ipld-eth-statedb/internal"
|
util "github.com/cerc-io/ipld-eth-statedb/internal"
|
||||||
|
"github.com/cerc-io/ipld-eth-statedb/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -41,13 +42,13 @@ type StateDatabase interface {
|
|||||||
var _ StateDatabase = &stateDatabase{}
|
var _ StateDatabase = &stateDatabase{}
|
||||||
|
|
||||||
type stateDatabase struct {
|
type stateDatabase struct {
|
||||||
db Database
|
db sql.Database
|
||||||
codeSizeCache *lru.Cache
|
codeSizeCache *lru.Cache
|
||||||
codeCache *fastcache.Cache
|
codeCache *fastcache.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewStateDatabase returns a new Database implementation using the passed parameters
|
// 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)
|
csc, _ := lru.New(codeSizeCacheSize)
|
||||||
return &stateDatabase{
|
return &stateDatabase{
|
||||||
db: db,
|
db: db,
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb_test
|
package state_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@ -16,8 +16,9 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
|
"github.com/ethereum/go-ethereum/statediff/indexer/database/sql/postgres"
|
||||||
"github.com/ethereum/go-ethereum/statediff/indexer/ipld"
|
"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"
|
util "github.com/cerc-io/ipld-eth-statedb/internal"
|
||||||
|
"github.com/cerc-io/ipld-eth-statedb/sql"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -52,7 +53,7 @@ var (
|
|||||||
|
|
||||||
AccountPK, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
AccountPK, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
||||||
AccountAddress = crypto.PubkeyToAddress(AccountPK.PublicKey) //0x703c4b2bD70c169f5717101CaeE543299Fc946C7
|
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}
|
AccountCode = []byte{0, 1, 2, 3, 4, 5, 6, 7}
|
||||||
AccountCodeHash = crypto.Keccak256Hash(AccountCode)
|
AccountCodeHash = crypto.Keccak256Hash(AccountCode)
|
||||||
@ -73,7 +74,7 @@ var (
|
|||||||
accountRLP, _ = rlp.EncodeToBytes(&Account)
|
accountRLP, _ = rlp.EncodeToBytes(&Account)
|
||||||
accountAndLeafRLP, _ = rlp.EncodeToBytes(&[]interface{}{AccountLeafKey, accountRLP})
|
accountAndLeafRLP, _ = rlp.EncodeToBytes(&[]interface{}{AccountLeafKey, accountRLP})
|
||||||
AccountCID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, accountAndLeafRLP, multihash.KECCAK_256)
|
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)
|
StoredValueRLP, _ = rlp.EncodeToBytes(StoredValue)
|
||||||
StoredValueRLP2, _ = rlp.EncodeToBytes("something")
|
StoredValueRLP2, _ = rlp.EncodeToBytes("something")
|
||||||
@ -108,11 +109,10 @@ func TestPGXSuite(t *testing.T) {
|
|||||||
require.NoError(t, tx.Commit(testCtx))
|
require.NoError(t, tx.Commit(testCtx))
|
||||||
})
|
})
|
||||||
|
|
||||||
driver := statedb.NewPGXDriverFromPool(context.Background(), pool)
|
database := sql.NewPGXDriverFromPool(context.Background(), pool)
|
||||||
database := statedb.NewPostgresDB(driver)
|
|
||||||
insertSuiteData(t, database)
|
insertSuiteData(t, database)
|
||||||
|
|
||||||
db := statedb.NewStateDatabase(database)
|
db := state.NewStateDatabase(database)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
testSuite(t, db)
|
testSuite(t, db)
|
||||||
}
|
}
|
||||||
@ -140,16 +140,15 @@ func TestSQLXSuite(t *testing.T) {
|
|||||||
require.NoError(t, tx.Commit())
|
require.NoError(t, tx.Commit())
|
||||||
})
|
})
|
||||||
|
|
||||||
driver := statedb.NewSQLXDriverFromPool(context.Background(), pool)
|
database := sql.NewSQLXDriverFromPool(context.Background(), pool)
|
||||||
database := statedb.NewPostgresDB(driver)
|
|
||||||
insertSuiteData(t, database)
|
insertSuiteData(t, database)
|
||||||
|
|
||||||
db := statedb.NewStateDatabase(database)
|
db := state.NewStateDatabase(database)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
testSuite(t, db)
|
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, BlockHash.String(), BlockParentHash.String(), BlockNumber.Uint64()))
|
||||||
require.NoError(t, insertHeaderCID(database, BlockHash2.String(), BlockHash.String(), BlockNumber2))
|
require.NoError(t, insertHeaderCID(database, BlockHash2.String(), BlockHash.String(), BlockNumber2))
|
||||||
require.NoError(t, insertHeaderCID(database, BlockHash3.String(), BlockHash2.String(), BlockNumber3))
|
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))
|
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) {
|
t.Run("Database", func(t *testing.T) {
|
||||||
size, err := db.ContractCodeSize(AccountCodeHash)
|
size, err := db.ContractCodeSize(AccountCodeHash)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -296,7 +295,7 @@ func testSuite(t *testing.T, db statedb.StateDatabase) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
t.Run("StateDB", func(t *testing.T) {
|
t.Run("StateDB", func(t *testing.T) {
|
||||||
sdb, err := statedb.New(BlockHash, db)
|
sdb, err := state.New(BlockHash, db)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
checkAccountUnchanged := func() {
|
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())
|
cid, err := util.Keccak256ToCid(ipld.MEthHeader, common.HexToHash(blockHash).Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -395,7 +394,7 @@ type stateModel struct {
|
|||||||
Removed bool
|
Removed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertStateCID(db statedb.Database, cidModel stateModel) error {
|
func insertStateCID(db sql.Database, cidModel stateModel) error {
|
||||||
sql := `INSERT INTO eth.state_cids (
|
sql := `INSERT INTO eth.state_cids (
|
||||||
block_number,
|
block_number,
|
||||||
header_id,
|
header_id,
|
||||||
@ -434,7 +433,7 @@ type storageModel struct {
|
|||||||
Removed bool
|
Removed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func insertStorageCID(db statedb.Database, cidModel storageModel) error {
|
func insertStorageCID(db sql.Database, cidModel storageModel) error {
|
||||||
sql := `INSERT INTO eth.storage_cids (
|
sql := `INSERT INTO eth.storage_cids (
|
||||||
block_number,
|
block_number,
|
||||||
header_id,
|
header_id,
|
||||||
@ -458,7 +457,7 @@ func insertStorageCID(db statedb.Database, cidModel storageModel) error {
|
|||||||
return err
|
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)`
|
sql := `INSERT INTO ipld.blocks (block_number, key, data) VALUES ($1, $2, $3)`
|
||||||
_, err := db.Exec(testCtx, sql, BlockNumber.Uint64(), AccountCodeCID.String(), AccountCode)
|
_, err := db.Exec(testCtx, sql, BlockNumber.Uint64(), AccountCodeCID.String(), AccountCode)
|
||||||
return err
|
return err
|
@ -14,7 +14,7 @@
|
|||||||
// You should have received a copy of the GNU Lesser General Public License
|
// You should have received a copy of the GNU Lesser General Public License
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
package ipld_eth_statedb
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
@ -1,14 +1,11 @@
|
|||||||
package ipld_eth_statedb
|
package sql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Database interfaces to support multiple Postgres drivers
|
// Database interfaces to support multiple Postgres drivers
|
||||||
type Database interface {
|
type Database = Driver
|
||||||
Driver
|
|
||||||
Statements
|
|
||||||
}
|
|
||||||
|
|
||||||
// Driver interface has all the methods required by a driver implementation to support the sql indexer
|
// Driver interface has all the methods required by a driver implementation to support the sql indexer
|
||||||
type Driver interface {
|
type Driver interface {
|
||||||
@ -25,10 +22,3 @@ type ScannableRow interface {
|
|||||||
type Result interface {
|
type Result interface {
|
||||||
RowsAffected() (int64, error)
|
RowsAffected() (int64, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Statements interface to accommodate different SQL query syntax
|
|
||||||
type Statements interface {
|
|
||||||
GetContractCodeStmt() string
|
|
||||||
GetStateAccountStmt() string
|
|
||||||
GetStorageSlotStmt() string
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package sql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
@ -1,4 +1,4 @@
|
|||||||
package ipld_eth_statedb
|
package sql
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
Loading…
Reference in New Issue
Block a user