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

348 lines
10 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"
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"
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"
2019-08-14 18:49:30 +00:00
log "github.com/sirupsen/logrus"
"github.com/vulcanize/vulcanizedb/pkg/ipfs"
2019-04-30 17:48:31 +00:00
)
// Test variables
var (
2019-08-14 18:49:30 +00:00
// block data
BlockNumber = big.NewInt(rand2.Int63())
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)
MockHeaderRlp, err = rlp.EncodeToBytes(MockBlock.Header())
MockTrxMeta = []*ipfs.TrxMetaData{
{
CID: "", // This is empty until we go to publish to ipfs
Src: senderAddr.Hex(),
Dst: "0x0000000000000000000000000000000000000000",
},
{
CID: "",
Src: senderAddr.Hex(),
Dst: "0x0000000000000000000000000000000000000001",
},
}
MockRctMeta = []*ipfs.ReceiptMetaData{
{
CID: "",
Topic0s: []string{
"0x0000000000000000000000000000000000000000000000000000000000000004",
},
ContractAddress: "0x0000000000000000000000000000000000000000",
},
{
CID: "",
Topic0s: []string{
"0x0000000000000000000000000000000000000000000000000000000000000005",
},
ContractAddress: "0x0000000000000000000000000000000000000001",
},
}
// 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)
2019-08-28 18:41:49 +00:00
Address = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592")
ContractLeafKey = ipfs.AddressToKey(Address)
AnotherAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593")
AnotherContractLeafKey = ipfs.AddressToKey(AnotherAddress)
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,
}
valueBytes, _ = rlp.EncodeToBytes(testAccount)
anotherValueBytes, _ = rlp.EncodeToBytes(anotherTestAccount)
CreatedAccountDiffs = []statediff.AccountDiff{
{
2019-04-30 17:48:31 +00:00
Key: ContractLeafKey.Bytes(),
Value: valueBytes,
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-14 18:49:30 +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
},
}
UpdatedAccountDiffs = []statediff.AccountDiff{{
2019-04-30 17:48:31 +00:00
Key: ContractLeafKey.Bytes(),
Value: valueBytes,
Storage: storage,
2019-08-14 18:49:30 +00:00
Leaf: true,
2019-04-30 17:48:31 +00:00
}}
DeletedAccountDiffs = []statediff.AccountDiff{{
2019-04-30 17:48:31 +00:00
Key: ContractLeafKey.Bytes(),
Value: valueBytes,
Storage: storage,
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)
2019-08-14 18:49:30 +00:00
MockStateNodes = map[common.Hash]ipfs.StateNode{
ContractLeafKey: {
Value: valueBytes,
Leaf: true,
},
AnotherContractLeafKey: {
Value: anotherValueBytes,
Leaf: true,
},
}
MockStorageNodes = map[common.Hash][]ipfs.StorageNode{
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{
2019-04-30 17:48:31 +00:00
BlockRlp: MockBlockRlp,
StateDiffRlp: MockStateDiffBytes,
2019-08-14 18:49:30 +00:00
ReceiptsRlp: ReceiptsRlp,
2019-04-30 17:48:31 +00:00
}
2019-08-14 18:49:30 +00:00
EmptyStateDiffPayload = statediff.Payload{
2019-04-30 17:48:31 +00:00
BlockRlp: []byte{},
StateDiffRlp: []byte{},
2019-08-14 18:49:30 +00:00
ReceiptsRlp: []byte{},
2019-04-30 17:48:31 +00:00
}
2019-08-26 02:13:40 +00:00
MockIPLDPayload = &ipfs.IPLDPayload{
BlockNumber: big.NewInt(1),
BlockHash: MockBlock.Hash(),
Receipts: MockReceipts,
HeaderRLP: MockHeaderRlp,
BlockBody: MockBlock.Body(),
TrxMetaData: []*ipfs.TrxMetaData{
{
CID: "",
Src: senderAddr.Hex(),
Dst: "0x0000000000000000000000000000000000000000",
},
{
CID: "",
Src: senderAddr.Hex(),
Dst: "0x0000000000000000000000000000000000000001",
},
},
ReceiptMetaData: []*ipfs.ReceiptMetaData{
{
CID: "",
Topic0s: []string{
"0x0000000000000000000000000000000000000000000000000000000000000004",
},
ContractAddress: "0x0000000000000000000000000000000000000000",
},
{
CID: "",
Topic0s: []string{
"0x0000000000000000000000000000000000000000000000000000000000000005",
},
ContractAddress: "0x0000000000000000000000000000000000000001",
},
},
StorageNodes: MockStorageNodes,
StateNodes: MockStateNodes,
2019-04-30 17:48:31 +00:00
}
2019-08-26 02:13:40 +00:00
MockCIDPayload = &ipfs.CIDPayload{
2019-04-30 17:48:31 +00:00
BlockNumber: "1",
2019-08-26 02:13:40 +00:00
BlockHash: MockBlock.Hash(),
2019-04-30 17:48:31 +00:00
HeaderCID: "mockHeaderCID",
2019-08-26 02:13:40 +00:00
UncleCIDS: make(map[common.Hash]string),
2019-04-30 17:48:31 +00:00
TransactionCIDs: map[common.Hash]*ipfs.TrxMetaData{
2019-08-26 02:13:40 +00:00
MockTransactions[0].Hash(): {
CID: "mockTrxCID1",
2019-08-26 02:13:40 +00:00
Dst: "0x0000000000000000000000000000000000000000",
Src: senderAddr.Hex(),
2019-04-30 17:48:31 +00:00
},
2019-08-26 02:13:40 +00:00
MockTransactions[1].Hash(): {
CID: "mockTrxCID2",
2019-08-26 02:13:40 +00:00
Dst: "0x0000000000000000000000000000000000000001",
Src: senderAddr.Hex(),
2019-04-30 17:48:31 +00:00
},
},
ReceiptCIDs: map[common.Hash]*ipfs.ReceiptMetaData{
2019-08-26 02:13:40 +00:00
MockTransactions[0].Hash(): {
CID: "mockRctCID1",
Topic0s: []string{"0x0000000000000000000000000000000000000000000000000000000000000004"},
ContractAddress: "0x0000000000000000000000000000000000000000",
2019-04-30 17:48:31 +00:00
},
2019-08-26 02:13:40 +00:00
MockTransactions[1].Hash(): {
CID: "mockRctCID2",
Topic0s: []string{"0x0000000000000000000000000000000000000000000000000000000000000005"},
ContractAddress: "0x0000000000000000000000000000000000000001",
2019-04-30 17:48:31 +00:00
},
},
StateNodeCIDs: map[common.Hash]ipfs.StateNodeCID{
2019-08-26 02:13:40 +00:00
ContractLeafKey: {
CID: "mockStateCID1",
Leaf: true,
2019-08-26 02:13:40 +00:00
Key: "",
},
2019-08-26 02:13:40 +00:00
AnotherContractLeafKey: {
CID: "mockStateCID2",
Leaf: true,
2019-08-26 02:13:40 +00:00
Key: "",
},
2019-04-30 17:48:31 +00:00
},
StorageNodeCIDs: map[common.Hash][]ipfs.StorageNodeCID{
2019-08-26 02:13:40 +00:00
ContractLeafKey: {
{
2019-08-26 02:13:40 +00:00
CID: "mockStorageCID",
Key: "0x0000000000000000000000000000000000000000000000000000000000000001",
Leaf: true,
StateKey: "",
},
2019-04-30 17:48:31 +00:00
},
},
}
2019-08-28 18:41:49 +00:00
MockCIDWrapper = &ipfs.CIDWrapper{
BlockNumber: big.NewInt(1),
Headers: []string{"mockHeaderCID"},
Transactions: []string{"mockTrxCID1", "mockTrxCID2"},
Receipts: []string{"mockRctCID1", "mockRctCID2"},
Uncles: []string{},
StateNodes: []ipfs.StateNodeCID{
{
CID: "mockStateCID1",
Leaf: true,
Key: ContractLeafKey.Hex(),
},
{
CID: "mockStateCID2",
Leaf: true,
Key: AnotherContractLeafKey.Hex(),
},
},
StorageNodes: []ipfs.StorageNodeCID{
{
CID: "mockStorageCID",
Leaf: true,
StateKey: ContractLeafKey.Hex(),
Key: "0x0000000000000000000000000000000000000000000000000000000000000001",
},
},
}
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, common.HexToAddress("0x0"), big.NewInt(1000), 50, big.NewInt(100), nil)
trx2 := types.NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(2000), 100, big.NewInt(200), nil)
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
mockTopic1 := common.HexToHash("0x04")
mockReceipt1 := types.NewReceipt(common.HexToHash("0x0").Bytes(), false, 50)
mockReceipt1.ContractAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476592")
mockLog1 := &types.Log{
Topics: []common.Hash{mockTopic1},
}
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
mockTopic2 := common.HexToHash("0x05")
mockReceipt2 := types.NewReceipt(common.HexToHash("0x1").Bytes(), false, 100)
mockReceipt2.ContractAddress = common.HexToAddress("0xaE9BEa628c4Ce503DcFD7E305CaB4e29E7476593")
mockLog2 := &types.Log{
Topics: []common.Hash{mockTopic2},
}
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
}