Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integration tests #78

Merged
merged 17 commits into from
Dec 20, 2023
6 changes: 3 additions & 3 deletions cmd/es-utils/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,10 @@ func UploadBlobs(
needEncoding,
-1,
value,
1000000,
5000000,
"",
"400000000",
"4000000000",
"",
"300000000",
chainID,
calldata,
)
Expand Down
4 changes: 2 additions & 2 deletions ethstorage/miner/l1_mining_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (m *l1MiningAPI) estimateReward(ctx context.Context, cfg Config, contract c
}

minedTs := curBlock.Time - (new(big.Int).Sub(curBlock.Number, block).Uint64())*12
var reward *big.Int
reward := big.NewInt(0)
if shard < lastShard {
basePayment := new(big.Int).Mul(cfg.StorageCost, new(big.Int).SetUint64(cfg.ShardEntry))
reward = paymentIn(basePayment, cfg.DcfFactor, lastMineTime, minedTs, cfg.StartTime)
Expand All @@ -236,7 +236,7 @@ func (m *l1MiningAPI) estimateReward(ctx context.Context, cfg Config, contract c
}
}
minerReward := new(big.Int).Div(
new(big.Int).Mul(new(big.Int).SetUint64((rewardDenominator-cfg.TreasuryShare)), reward),
new(big.Int).Mul(new(big.Int).SetUint64(rewardDenominator-cfg.TreasuryShare), reward),
new(big.Int).SetUint64(rewardDenominator),
)
return minerReward, nil
Expand Down
99 changes: 5 additions & 94 deletions ethstorage/miner/miner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
package miner

import (
"context"
"fmt"
"io"
"math/big"
"net/http"
"os"
"path/filepath"
"runtime/debug"
Expand All @@ -19,113 +15,42 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/event"
"github.com/ethstorage/go-ethstorage/cmd/es-utils/utils"
es "github.com/ethstorage/go-ethstorage/ethstorage"
"github.com/ethstorage/go-ethstorage/ethstorage/eth"
esLog "github.com/ethstorage/go-ethstorage/ethstorage/log"
"github.com/ethstorage/go-ethstorage/ethstorage/p2p/protocol"
"github.com/ethstorage/go-ethstorage/ethstorage/prover"
"github.com/ethstorage/go-ethstorage/ethstorage/signer"
"golang.org/x/term"
)

const (
rpcUrl = "http://65.108.236.27:8545"
chainID = "7011893058"
kvSizeBits = 17
sampleLenBits = 12
kvEntriesBits = 1
private = "95eb6ffd2ae0b115db4d1f0d58388216f9d026896696a5211d77b5f14eb5badf"
)

var (
// Note a new contract need to be deployed each time the test is being executed.
contractAddr = common.HexToAddress("0x8FA1872c159DD8681119000d1C7a8Df52a8C128F")
minerAddr = common.HexToAddress("0x04580493117292ba13361D8e9e28609ec112264D")
fileName = "test_shard_0.dat"
kvSize uint64 = 1 << kvSizeBits
kvEntries uint64 = 1 << kvEntriesBits
shardID = uint64(0)
value = hexutil.EncodeUint64(10000000000000)
lg = esLog.NewLogger(esLog.CLIConfig{
Level: "debug",
Format: "text",
Color: term.IsTerminal(int(os.Stdout.Fd())),
})
lg = esLog.NewLogger(esLog.DefaultCLIConfig())
)

func TestMine(t *testing.T) {
client, err := eth.Dial(rpcUrl, contractAddr, lg)
if err != nil {
t.Fatalf("failed to get client L1: %v", err)
}
storageMgr := initStorageManager(t, client, true)
defer func() {
client.Close()
storageMgr.Close()
err := os.Remove(fileName)
if err != nil {
t.Errorf("cannot remove %s: %v", fileName, err)
}
}()
miner := newMiner(t, storageMgr, client)
eth.WatchHeadChanges(context.Background(), client, func(ctx context.Context, sig eth.L1BlockRef) {
select {
case miner.ChainHeadCh <- sig:
default:
}
})
miner.Start()
miner.feed.Send(protocol.EthStorageSyncDone{
DoneType: protocol.SingleShardDone,
ShardId: shardID,
})
time.Sleep(300 * time.Second)
miner.Close()
checkMiningState(t, miner, false)
}

func initStorageManager(t *testing.T, client *eth.PollingClient, withData bool) *es.StorageManager {
func initStorageManager(t *testing.T, client *eth.PollingClient) *es.StorageManager {
df, err := es.Create(fileName, shardID, kvEntries, 0, kvSize, es.ENCODE_BLOB_POSEIDON, minerAddr, kvSize)
if err != nil {
t.Fatalf("Create failed %v", err)
}
shardMgr := es.NewShardManager(contractAddr, kvSize, kvEntries, kvSize)
shardMgr.AddDataShard(shardID)
shardMgr.AddDataFile(df)
if withData {
dataRaw, err := readFile()
if err != nil {
t.Fatalf("Read raw data failed %v", err)
}
kvIdxes, dataHashes, err := utils.UploadBlobs(client, rpcUrl, private, chainID, contractAddr, dataRaw, true, value)
if err != nil {
t.Fatalf("Upload blobs failed %v", err)
}
blobs := utils.EncodeBlobs(dataRaw)
for i, blob := range blobs {
kvIdx := kvIdxes[i] % kvEntries
t.Logf("TryWrite kvIdx: %d kvHash: %x\n", kvIdx, dataHashes[i])
_, err := shardMgr.TryWrite(kvIdx, blob[:], dataHashes[i])
if err != nil {
t.Fatalf("write failed: %v", err)
}
}
}
return es.NewStorageManager(shardMgr, client)
}

func newMiner(t *testing.T, storageMgr *es.StorageManager, client *eth.PollingClient) *Miner {
key, _ := crypto.HexToECDSA(private)
signerAddr := crypto.PubkeyToAddress(key.PublicKey)
signerFnFactory, signerAddr, err := signer.SignerFactoryFromConfig(
signer.CLIConfig{},
)
if err != nil {
t.Fatalf("failed to get signer: %v", err)
}
defaultConfig := &Config{
RandomChecks: 2,
NonceLimit: 1048576,
Expand All @@ -136,8 +61,6 @@ func newMiner(t *testing.T, storageMgr *es.StorageManager, client *eth.PollingCl
PriorityGasPrice: new(big.Int).SetUint64(10),
ThreadsPerShard: 1,
ZKeyFileName: "blob_poseidon.zkey",
SignerFnFactory: signerFnFactory,
SignerAddr: signerAddr,
}
l1api := NewL1MiningAPI(client, lg)
zkWorkingDir, _ := filepath.Abs("../prover")
Expand All @@ -159,24 +82,10 @@ func checkMiningState(t *testing.T, m *Miner, mining bool) {
t.Fatalf("Mining() == %t, want %t", state, mining)
}

func readFile() ([]byte, error) {
txt_170k := "https://www.gutenberg.org/cache/epub/11/pg11.txt"
resp, err := http.Get(txt_170k)
if err != nil {
return nil, fmt.Errorf("error reading blob txtUrl: %v", err)
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading blob txtUrl: %v", err)
}
return data, nil
}

func TestMiner_update(t *testing.T) {
var shard = []uint64{0, 1, 2}
// Case: happy path
storageMgr := initStorageManager(t, nil, false)
storageMgr := initStorageManager(t, nil)
miner := newMiner(t, storageMgr, nil)
miner.Start()
// waiting for sync done
Expand Down Expand Up @@ -223,4 +132,6 @@ func TestMiner_update(t *testing.T) {
}
miner.Close()
checkMiningState(t, miner, false)
storageMgr.Close()
os.Remove(fileName)
}
Loading