ipld-eth-server/pkg/super_node/eth/mocks/test_data.go

391 lines
12 KiB
Go
Raw Normal View History

2019-04-30 17:48:31 +00:00
// VulcanizeDB
// Copyright © 2019 Vulcanize
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program 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 Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package mocks
2019-04-30 17:48:31 +00:00
import (
2019-08-14 18:49:30 +00:00
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
2019-04-30 17:48:31 +00:00
"math/big"
2019-08-14 18:49:30 +00:00
rand2 "math/rand"
"github.com/multiformats/go-multihash"
"github.com/vulcanize/vulcanizedb/pkg/ipfs/ipld"
2019-04-30 17:48:31 +00:00
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
2019-08-14 18:49:30 +00:00
"github.com/ethereum/go-ethereum/params"
2019-04-30 17:48:31 +00:00
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/statediff"
"github.com/ipfs/go-block-format"
2019-08-14 18:49:30 +00:00
log "github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/super_node/eth"
eth2 "github.com/vulcanize/vulcanizedb/pkg/super_node/eth"
2019-04-30 17:48:31 +00:00
)
// Test variables
var (
2019-08-14 18:49:30 +00:00
// block data
BlockNumber = big.NewInt(1)
2019-08-14 18:49:30 +00:00
MockHeader = types.Header{
Time: 0,
Number: BlockNumber,
Root: common.HexToHash("0x0"),
TxHash: common.HexToHash("0x0"),
ReceiptHash: common.HexToHash("0x0"),
}
MockTransactions, MockReceipts, senderAddr = createTransactionsAndReceipts()
ReceiptsRlp, _ = rlp.EncodeToBytes(MockReceipts)
MockBlock = types.NewBlock(&MockHeader, MockTransactions, nil, MockReceipts)
MockBlockRlp, _ = rlp.EncodeToBytes(MockBlock)
2019-08-28 19:43:27 +00:00
MockHeaderRlp, _ = rlp.EncodeToBytes(MockBlock.Header())
Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592")
AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593")
mockTopic11 = common.HexToHash("0x04")
mockTopic12 = common.HexToHash("0x06")
mockTopic21 = common.HexToHash("0x05")
mockTopic22 = common.HexToHash("0x07")
MockTrxMeta = []eth.TxModel{
2019-08-14 18:49:30 +00:00
{
CID: "", // This is empty until we go to publish to ipfs
Src: senderAddr.Hex(),
Dst: Address.String(),
Index: 0,
TxHash: MockTransactions[0].Hash().String(),
2019-08-14 18:49:30 +00:00
},
{
CID: "",
Src: senderAddr.Hex(),
Dst: AnotherAddress.String(),
Index: 1,
TxHash: MockTransactions[1].Hash().String(),
},
}
MockTrxMetaPostPublsh = []eth.TxModel{
{
CID: "mockTrxCID1", // This is empty until we go to publish to ipfs
Src: senderAddr.Hex(),
Dst: Address.String(),
Index: 0,
TxHash: MockTransactions[0].Hash().String(),
},
{
CID: "mockTrxCID2",
Src: senderAddr.Hex(),
Dst: AnotherAddress.String(),
Index: 1,
TxHash: MockTransactions[1].Hash().String(),
2019-08-14 18:49:30 +00:00
},
}
MockRctMeta = []eth.ReceiptModel{
2019-08-14 18:49:30 +00:00
{
CID: "",
Topic0s: []string{
mockTopic11.String(),
},
Topic1s: []string{
mockTopic12.String(),
2019-08-14 18:49:30 +00:00
},
Contract: Address.String(),
2019-08-14 18:49:30 +00:00
},
{
CID: "",
Topic0s: []string{
mockTopic21.String(),
2019-08-14 18:49:30 +00:00
},
Topic1s: []string{
mockTopic22.String(),
},
Contract: AnotherAddress.String(),
},
}
MockRctMetaPostPublish = []eth.ReceiptModel{
{
CID: "mockRctCID1",
Topic0s: []string{
mockTopic11.String(),
},
Topic1s: []string{
mockTopic12.String(),
},
Contract: Address.String(),
},
{
CID: "mockRctCID2",
Topic0s: []string{
mockTopic21.String(),
},
Topic1s: []string{
mockTopic22.String(),
},
Contract: AnotherAddress.String(),
2019-08-14 18:49:30 +00:00
},
}
// statediff data
CodeHash = common.Hex2Bytes("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")
NonceValue = rand2.Uint64()
anotherNonceValue = rand2.Uint64()
BalanceValue = rand2.Int63()
anotherBalanceValue = rand2.Int63()
ContractRoot = common.HexToHash("0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
StoragePath = common.HexToHash("0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470").Bytes()
StorageKey = common.HexToHash("0000000000000000000000000000000000000000000000000000000000000001").Bytes()
StorageValue = common.Hex2Bytes("0x03")
storage = []statediff.StorageDiff{{
2019-04-30 17:48:31 +00:00
Key: StorageKey,
Value: StorageValue,
Path: StoragePath,
Proof: [][]byte{},
2019-08-14 18:49:30 +00:00
Leaf: true,
2019-04-30 17:48:31 +00:00
}}
emptyStorage = make([]statediff.StorageDiff, 0)
ContractLeafKey = crypto.Keccak256Hash(Address.Bytes())
AnotherContractLeafKey = crypto.Keccak256Hash(AnotherAddress.Bytes())
2019-04-30 17:48:31 +00:00
testAccount = state.Account{
2019-08-14 18:49:30 +00:00
Nonce: NonceValue,
Balance: big.NewInt(BalanceValue),
2019-04-30 17:48:31 +00:00
Root: ContractRoot,
CodeHash: CodeHash,
}
2019-08-14 18:49:30 +00:00
anotherTestAccount = state.Account{
Nonce: anotherNonceValue,
Balance: big.NewInt(anotherBalanceValue),
Root: common.HexToHash("0x"),
CodeHash: nil,
}
2019-08-28 19:43:27 +00:00
ValueBytes, _ = rlp.EncodeToBytes(testAccount)
AnotherValueBytes, _ = rlp.EncodeToBytes(anotherTestAccount)
2019-08-14 18:49:30 +00:00
CreatedAccountDiffs = []statediff.AccountDiff{
{
2019-04-30 17:48:31 +00:00
Key: ContractLeafKey.Bytes(),
2019-08-28 19:43:27 +00:00
Value: ValueBytes,
2019-04-30 17:48:31 +00:00
Storage: storage,
2019-08-14 18:49:30 +00:00
Leaf: true,
2019-04-30 17:48:31 +00:00
},
{
2019-04-30 17:48:31 +00:00
Key: AnotherContractLeafKey.Bytes(),
2019-08-28 19:43:27 +00:00
Value: AnotherValueBytes,
2019-04-30 17:48:31 +00:00
Storage: emptyStorage,
2019-08-14 18:49:30 +00:00
Leaf: true,
2019-04-30 17:48:31 +00:00
},
}
MockStateDiff = statediff.StateDiff{
BlockNumber: BlockNumber,
2019-08-14 18:49:30 +00:00
BlockHash: MockBlock.Hash(),
2019-04-30 17:48:31 +00:00
CreatedAccounts: CreatedAccountDiffs,
}
MockStateDiffBytes, _ = rlp.EncodeToBytes(MockStateDiff)
MockStateNodes = []eth.TrieNode{
{
Key: ContractLeafKey,
2019-08-28 19:43:27 +00:00
Value: ValueBytes,
2019-08-14 18:49:30 +00:00
Leaf: true,
},
{
Key: AnotherContractLeafKey,
2019-08-28 19:43:27 +00:00
Value: AnotherValueBytes,
2019-08-14 18:49:30 +00:00
Leaf: true,
},
}
MockStateMetaPostPublish = []eth.StateNodeModel{
{
CID: "mockStateCID1",
Leaf: true,
StateKey: ContractLeafKey.String(),
},
{
CID: "mockStateCID2",
Leaf: true,
StateKey: AnotherContractLeafKey.String(),
},
}
MockStorageNodes = map[common.Hash][]eth.TrieNode{
2019-08-14 18:49:30 +00:00
ContractLeafKey: {
{
Key: common.BytesToHash(StorageKey),
Value: StorageValue,
Leaf: true,
},
},
2019-04-30 17:48:31 +00:00
}
2019-08-14 18:49:30 +00:00
// aggregate payloads
MockStateDiffPayload = statediff.Payload{
BlockRlp: MockBlockRlp,
StateDiffRlp: MockStateDiffBytes,
ReceiptsRlp: ReceiptsRlp,
TotalDifficulty: big.NewInt(1337),
2019-04-30 17:48:31 +00:00
}
MockConvertedPayload = eth.ConvertedPayload{
TotalDifficulty: big.NewInt(1337),
Block: MockBlock,
Receipts: MockReceipts,
2020-01-29 20:37:20 +00:00
TxMetaData: MockTrxMeta,
ReceiptMetaData: MockRctMeta,
StorageNodes: MockStorageNodes,
StateNodes: MockStateNodes,
2019-04-30 17:48:31 +00:00
}
MockCIDPayload = &eth.CIDPayload{
HeaderCID: eth2.HeaderModel{
BlockHash: MockBlock.Hash().String(),
BlockNumber: MockBlock.Number().String(),
CID: "mockHeaderCID",
ParentHash: MockBlock.ParentHash().String(),
TotalDifficulty: "1337",
},
UncleCIDs: []eth2.UncleModel{},
TransactionCIDs: MockTrxMetaPostPublsh,
ReceiptCIDs: map[common.Hash]eth.ReceiptModel{
MockTransactions[0].Hash(): MockRctMetaPostPublish[0],
MockTransactions[1].Hash(): MockRctMetaPostPublish[1],
2019-04-30 17:48:31 +00:00
},
StateNodeCIDs: MockStateMetaPostPublish,
StorageNodeCIDs: map[common.Hash][]eth.StorageNodeModel{
2019-08-26 02:13:40 +00:00
ContractLeafKey: {
{
CID: "mockStorageCID",
StorageKey: "0x0000000000000000000000000000000000000000000000000000000000000001",
Leaf: true,
},
2019-04-30 17:48:31 +00:00
},
},
}
2019-08-28 18:41:49 +00:00
MockCIDWrapper = &eth.CIDWrapper{
BlockNumber: big.NewInt(1),
Headers: []eth2.HeaderModel{
{
BlockNumber: "1",
BlockHash: MockBlock.Hash().String(),
ParentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
CID: "mockHeaderCID",
TotalDifficulty: "1337",
},
},
Transactions: MockTrxMetaPostPublsh,
Receipts: MockRctMetaPostPublish,
Uncles: []eth2.UncleModel{},
StateNodes: MockStateMetaPostPublish,
StorageNodes: []eth.StorageNodeWithStateKeyModel{
{
CID: "mockStorageCID",
Leaf: true,
StateKey: ContractLeafKey.Hex(),
StorageKey: "0x0000000000000000000000000000000000000000000000000000000000000001",
2019-08-28 18:41:49 +00:00
},
},
}
headerCID, _ = ipld.RawdataToCid(ipld.MEthHeader, MockHeaderRlp, multihash.KECCAK_256)
trx1CID, _ = ipld.RawdataToCid(ipld.MEthTx, MockTransactions.GetRlp(0), multihash.KECCAK_256)
trx2CID, _ = ipld.RawdataToCid(ipld.MEthTx, MockTransactions.GetRlp(1), multihash.KECCAK_256)
rct1CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, MockReceipts.GetRlp(0), multihash.KECCAK_256)
rct2CID, _ = ipld.RawdataToCid(ipld.MEthTxReceipt, MockReceipts.GetRlp(1), multihash.KECCAK_256)
state1CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, ValueBytes, multihash.KECCAK_256)
state2CID, _ = ipld.RawdataToCid(ipld.MEthStateTrie, AnotherValueBytes, multihash.KECCAK_256)
storageCID, _ = ipld.RawdataToCid(ipld.MEthStorageTrie, StorageValue, multihash.KECCAK_256)
HeaderIPLD, _ = blocks.NewBlockWithCid(MockHeaderRlp, headerCID)
Trx1IPLD, _ = blocks.NewBlockWithCid(MockTransactions.GetRlp(0), trx1CID)
Trx2IPLD, _ = blocks.NewBlockWithCid(MockTransactions.GetRlp(1), trx2CID)
Rct1IPLD, _ = blocks.NewBlockWithCid(MockReceipts.GetRlp(0), rct1CID)
Rct2IPLD, _ = blocks.NewBlockWithCid(MockReceipts.GetRlp(1), rct2CID)
State1IPLD, _ = blocks.NewBlockWithCid(ValueBytes, state1CID)
State2IPLD, _ = blocks.NewBlockWithCid(AnotherValueBytes, state2CID)
StorageIPLD, _ = blocks.NewBlockWithCid(StorageValue, storageCID)
2019-08-28 19:43:27 +00:00
MockIPLDs = eth.IPLDs{
2019-08-28 19:43:27 +00:00
BlockNumber: big.NewInt(1),
Headers: [][]byte{
HeaderIPLD.RawData(),
2019-08-28 19:43:27 +00:00
},
Transactions: [][]byte{
Trx1IPLD.RawData(),
Trx2IPLD.RawData(),
2019-08-28 19:43:27 +00:00
},
Receipts: [][]byte{
Rct1IPLD.RawData(),
Rct2IPLD.RawData(),
2019-08-28 19:43:27 +00:00
},
StateNodes: []eth2.StateNode{
{
StateTrieKey: ContractLeafKey,
Leaf: true,
IPLD: State1IPLD.RawData(),
},
{
StateTrieKey: AnotherContractLeafKey,
Leaf: true,
IPLD: State2IPLD.RawData(),
2019-08-28 19:43:27 +00:00
},
},
StorageNodes: []eth2.StorageNode{
{
StateTrieKey: ContractLeafKey,
StorageTrieKey: common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000001"),
Leaf: true,
IPLD: StorageIPLD.RawData(),
2019-08-28 19:43:27 +00:00
},
},
}
2019-04-30 17:48:31 +00:00
)
2019-08-14 18:49:30 +00:00
// createTransactionsAndReceipts is a helper function to generate signed mock transactions and mock receipts with mock logs
func createTransactionsAndReceipts() (types.Transactions, types.Receipts, common.Address) {
// make transactions
trx1 := types.NewTransaction(0, Address, big.NewInt(1000), 50, big.NewInt(100), nil)
trx2 := types.NewTransaction(1, AnotherAddress, big.NewInt(2000), 100, big.NewInt(200), nil)
2019-08-14 18:49:30 +00:00
transactionSigner := types.MakeSigner(params.MainnetChainConfig, BlockNumber)
mockCurve := elliptic.P256()
mockPrvKey, err := ecdsa.GenerateKey(mockCurve, rand.Reader)
if err != nil {
log.Fatal(err)
}
signedTrx1, err := types.SignTx(trx1, transactionSigner, mockPrvKey)
if err != nil {
log.Fatal(err)
}
signedTrx2, err := types.SignTx(trx2, transactionSigner, mockPrvKey)
if err != nil {
log.Fatal(err)
}
senderAddr, err := types.Sender(transactionSigner, signedTrx1) // same for both trx
if err != nil {
log.Fatal(err)
}
// make receipts
mockReceipt1 := types.NewReceipt(common.HexToHash("0x0").Bytes(), false, 50)
mockLog1 := &types.Log{
Topics: []common.Hash{mockTopic11, mockTopic12},
2019-08-14 18:49:30 +00:00
}
mockReceipt1.Logs = []*types.Log{mockLog1}
2019-08-26 02:13:40 +00:00
mockReceipt1.TxHash = signedTrx1.Hash()
2019-08-14 18:49:30 +00:00
mockReceipt2 := types.NewReceipt(common.HexToHash("0x1").Bytes(), false, 100)
mockLog2 := &types.Log{
Topics: []common.Hash{mockTopic21, mockTopic22},
2019-08-14 18:49:30 +00:00
}
mockReceipt2.Logs = []*types.Log{mockLog2}
2019-08-26 02:13:40 +00:00
mockReceipt2.TxHash = signedTrx2.Hash()
2019-08-14 18:49:30 +00:00
return types.Transactions{signedTrx1, signedTrx2}, types.Receipts{mockReceipt1, mockReceipt2}, senderAddr
}